﻿using AutoMapper;
using Dapper;
using DocumentFormat.OpenXml.Bibliography;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Math;
using DocumentFormat.OpenXml.Wordprocessing;
using Hangfire;
using Hangfire.LiteDB;
using housingsafety.server.Caching.Core;
using housingsafety.server.Entities;
using housingsafety.server.Entities.DataModels;
using housingsafety.server.Helpers;
using housingsafety.server.Models;
using housingsafety.server.Repository;
using housingsafety.server.Repository.Extensions;
using housingsafety.server.Svcs;
using KS3;
using KS3.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Minio.DataModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Npgsql;
using NPOI.HPSF;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Asn1.Ocsp;
using Quartz.Util;
using RestSharp.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static housingsafety.server.Controllers.HouseMngCjCountController;
using static housingsafety.server.Controllers.HouseMngCountController;

namespace housingsafety.server.Controllers
{
    /// <summary>
    /// 信息统计  自查统计信息
    /// </summary>
    public class HouseMngCountController : BaseController<HouseMngController>
    {
        public IConfiguration Configuration { get; }
        private readonly ICache _cache;
        private readonly IMapper _mapper;
        private readonly IStorageProvider _storageProvider;
        private readonly ZmDbContext _dbContext;
        private readonly IRepository<ZmUser> _userRepository;
        private readonly IRepository<ZmBuildings> _buildingRepository;
        private readonly IRepository<ZmFileAttach> _fileAttachRepository;
        private readonly IOptions<StaticDataConf> _staticVtOptions;
        private readonly IRepository<ZmDepartment> _departmentRepository;
        private readonly IRepository<ZmPcYhLogDx> _zmPcYhLogDxRepository;
        private readonly IRepository<ZmPcYhLog> _zmPcYhLogRepository;
        private readonly IRepository<ZmPcYh> _zmPcYhRepository;
        private readonly IRepository<ZmPcBase> _zmPcBaseRepository;
        private readonly IRepository<ZmPcBaseLog> _zmPcBaseLogRepository;

        private readonly IDbTransaction _transaction;

        private readonly Microsoft.AspNetCore.Http.Features.FormOptions _defaultFormOptions = new Microsoft.AspNetCore.Http.Features.FormOptions();
        private readonly string[] _permittedExtensions = { ".mp3", ".mp4", ".jpg", ".jpeg", ".png", ".geojson", ".doc", ".docx" };

        private static object _lock = new object();

        private readonly KS3Client _kS3Client;


        public HouseMngCountController(IStorageProvider storageProvider
            , ZmDbContext dbContext
            , ILogger<HouseMngController> logger,
            IConfiguration configuration
            , IRepository<ZmSystemLog> systemLogRepository) : base(logger, systemLogRepository)
        {
            _dbContext = dbContext;
            _storageProvider = storageProvider;
            Configuration = configuration;
        }

        public class Param
        {
            public string id { get; set; }

            public string msgid { get; set; }

            public List<DateTime> Time { get; set; }

            public string villagedm { get; set; }
        }



        /// <summary>
        /// 清理电动车统计
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/ddzxcwgcd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> ddzxcwgcd([FromBody] Param Param)
        {
            var usernNumber = this.CurrentUser();
            try
            {

                object par = new { };

                string sql = @" select 
CONCAT('[', STRING_AGG(log_date, ', '), ']') AS x,
CONCAT('[', STRING_AGG(CAST(log_count AS VARCHAR), ', '), ']') AS y from (SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(CURRENT_DATE - INTERVAL '6 days', CURRENT_DATE, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlddcsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= CURRENT_DATE - INTERVAL '6 days' and b.villagedm=@village
          AND date(pcsj) < CURRENT_DATE
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a;
";
                string countsql = @" select 
sum(log_count) log_count
 from (SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(CURRENT_DATE - INTERVAL '6 days', CURRENT_DATE, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlddcsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= CURRENT_DATE - INTERVAL '6 days' and b.villagedm=@village
          AND date(pcsj) < CURRENT_DATE
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a;
";
                par = new { village = Param.villagedm };
                if (Param.Time != null)
                {
                    if (Param.Time.Count == 2)
                    {
                        TimeSpan difference = Param.Time[1].Date - Param.Time[0].Date;
                        int diff = (int)difference.TotalDays;
                        countsql = @$" select 
  sum(log_count) log_count
from  ( SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(@end - INTERVAL '{diff} days', @end, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlddcsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= @end - INTERVAL '{diff} days' and b.villagedm=@village
          AND date(pcsj) < @end
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a ";
                        sql = @$" select 
CONCAT('[', STRING_AGG(log_date, ', '), ']') AS x,
CONCAT('[', STRING_AGG(CAST(log_count AS VARCHAR), ', '), ']') AS y
from  ( SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(@end - INTERVAL '{diff} days', @end, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlddcsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= @end - INTERVAL '{diff} days' and b.villagedm=@village
          AND date(pcsj) < @end
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a ";
                        par = new { village = Param.villagedm, start = Param.Time[0], end = Param.Time[1] };
                    }
                }
                if (string.IsNullOrWhiteSpace(Param.villagedm))
                {
                    sql = sql.Replace("and b.villagedm=@village", "");
                    countsql = countsql.Replace("and b.villagedm=@village", "");
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, par).FirstOrDefault();
                int datacount = _dbContext.Database.GetDbConnection().Query<int>(countsql, par).FirstOrDefault();
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                if (data != null)
                {
                    var properties = data as IDictionary<string, object>;
                    foreach (var kvp in properties)
                    {
                        var stringlist = kvp.Value.ToString().Trim('[', ']').Split(", ");
                        var list = new List<string>();
                        foreach (var item in stringlist)
                        {
                            list.Add(item);
                        }
                        dictionary.Add(kvp.Key, list);
                    }
                    var r = new RespRet<object>(true, "Ok", new { data = dictionary, datacount = datacount });
                    WriteSystemLog("houses/ddzxcwgcd", r, Param);
                    return r;
                }
                return new RespRet<object>(true, "Ok", new { data = data, datacount = datacount });
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/ddzxcwgcd", res, Param);
                res.Msg = "系统异常";
                return res;
            }
        }


        /// <summary>
        /// 清理电动车统计
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/wgsypzyhsyq")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> wgsypzyhsyq([FromBody] Param Param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                object par = new { };
                string sql = @" select 
CONCAT('[', STRING_AGG(log_date, ', '), ']') AS x,
CONCAT('[', STRING_AGG(CAST(log_count AS VARCHAR), ', '), ']') AS y from (SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(CURRENT_DATE - INTERVAL '6 days', CURRENT_DATE, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlyhsyqsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= CURRENT_DATE - INTERVAL '6 days' and b.villagedm=@village
          AND date(pcsj) < CURRENT_DATE
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a;
";
                string countsql = @" select 
sum(log_count) log_count
 from (SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(CURRENT_DATE - INTERVAL '6 days', CURRENT_DATE, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlyhsyqsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= CURRENT_DATE - INTERVAL '6 days' and b.villagedm=@village
          AND date(pcsj) < CURRENT_DATE
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a;
";
                par = new { village = Param.villagedm };
                if (Param.Time != null)
                {
                    if (Param.Time.Count == 2)
                    {
                        TimeSpan difference = Param.Time[1].Date - Param.Time[0].Date;
                        int diff = (int)difference.TotalDays;
                        countsql = @$" select 
  sum(log_count) log_count
from  ( SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(@end - INTERVAL '{diff} days', @end, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlyhsyqsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= @end - INTERVAL '{diff} days' and b.villagedm=@village
          AND date(pcsj) < @end
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a ";
                        sql = @$" select 
CONCAT('[', STRING_AGG(log_date, ', '), ']') AS x,
CONCAT('[', STRING_AGG(CAST(log_count AS VARCHAR), ', '), ']') AS y
from  ( SELECT TO_CHAR(date_series.log_date, 'YYYY-MM-DD') log_date, COALESCE(log_count, 0) AS log_count
FROM generate_series(@end - INTERVAL '{diff} days', @end, INTERVAL '1 day') date_series(log_date)
LEFT JOIN (
    SELECT date(pcsj) AS log_date, sum(zxqlyhsyqsl) AS log_count
    FROM zm_pc_yhlog a
    LEFT JOIN zm_buildings b ON a.fwbh = b.fwbh
    WHERE date(pcsj) >= @end - INTERVAL '{diff} days' and b.villagedm=@village
          AND date(pcsj) < @end
    GROUP BY date(pcsj)
) AS logs ON date_series.log_date = logs.log_date
ORDER BY date_series.log_date desc)a ";
                        par = new { village = Param.villagedm, start = Param.Time[0], end = Param.Time[1] };
                    }
                }
                if (string.IsNullOrWhiteSpace(Param.villagedm))
                {
                    sql = sql.Replace("and b.villagedm=@village", "");
                    countsql = countsql.Replace("and b.villagedm=@village", "");
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, par).FirstOrDefault();
                int datacount = _dbContext.Database.GetDbConnection().Query<int>(countsql, par).FirstOrDefault();
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                if (data != null)
                {
                    var properties = data as IDictionary<string, object>;
                    foreach (var kvp in properties)
                    {
                        var stringlist = kvp.Value.ToString().Trim('[', ']').Split(", ");
                        var list = new List<string>();
                        foreach (var item in stringlist)
                        {
                            list.Add(item);
                        }
                        dictionary.Add(kvp.Key, list);
                    }
                    var r = new RespRet<object>(true, "Ok", new { data = dictionary, datacount = datacount });
                    WriteSystemLog("houses/wgsypzyhsyq", r, Param);
                    return r;
                }
                return new RespRet<object>(true, "Ok", new { data = data, datacount = datacount });
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/wgsypzyhsyq", res, Param);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 自定义sql
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/excute")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> excute([FromBody] Param Param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrWhiteSpace(Param.id))
                {
                    return new RespRet<object>(false, "id不能为空");
                }
                var sql = _dbContext.ZmSql.FirstOrDefault(x => x.Id == Param.id);
                var sqlParams = new List<NpgsqlParameter>();
                if (sql == null)
                {
                    return new RespRet<object>(false, "未找到可执行语句，请传递正确id");
                }
                object par = new { };
                if (string.IsNullOrWhiteSpace(Param.villagedm))
                {
                    sql.Value = sql.Value.Replace("and village=@village", " ");
                    sql.Value = sql.Value.Replace("and b.villagedm=@village", " ");
                }
                else
                {
                    par = new { village = Param.villagedm };
                }
                if (Param.Time != null)
                {
                    if (Param.Time.Count == 2)
                    {
                        par = new { village = Param.villagedm, start = Param.Time[0], end = Param.Time[1] };
                    }
                    else
                    {
                        sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                    }
                }
                else
                {
                    sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql.Value, par).First();
                return new RespRet<object>(true, "Ok", data);
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/excute", res, Param);
                res.Msg = "系统异常";
                return res;
            }
        }

        static List<string> ConvertObjectFieldsToList(object obj)
        {
            List<string> stringList = new List<string>();
            // Use reflection to get type information
            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo property in properties)
            {
                // Get the value of each property
                object value = property.Name; // Pass null for no parameters
                // Convert the value to string (or "null" if it's null)
                string stringValue = (value != null) ? value.ToString() : "null";
                // Add the string value to the list
                stringList.Add(stringValue);
            }

            return stringList;
        }



        static Dictionary<string, object> ConvertDynamicObjectToDictionary(dynamic obj)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            if (obj != null)
            {
                var properties = obj as IDictionary<string, object>;
                foreach (var kvp in properties)
                {
                    dictionary.Add(kvp.Key, kvp.Value);
                }
            }
            return dictionary;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/excutetb")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> Excutetb(Param param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrWhiteSpace(param.id))
                {
                    return new RespRet<object>(false, "id不能为空");
                }
                var sql = _dbContext.ZmSql.FirstOrDefault(x => x.Id == param.id);
                if (sql == null)
                {
                    return new RespRet<object>(false, "未找到可执行语句，请传递正确id");
                }
                object par = new { };
                if (string.IsNullOrWhiteSpace(param.villagedm))
                {
                    sql.Value = sql.Value.Replace("and village=@village", " ");
                }
                else
                {
                    par = new { village = param.villagedm };
                }
                if (param.Time != null)
                {
                    if (param.Time.Count == 2)
                    {
                        par = new { village = param.villagedm, start = param.Time[0], end = param.Time[1] };
                    }
                    else
                    {
                        sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                    }
                }
                else
                {
                    sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql.Value, par).FirstOrDefault();

                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                if (data != null)
                {
                    var properties = data as IDictionary<string, object>;
                    foreach (var kvp in properties)
                    {
                        var stringlist = kvp.Value.ToString().Trim('[', ']').Split(", ");
                        var list = new List<string>();
                        foreach (var item in stringlist)
                        {
                            list.Add(item);
                        }
                        dictionary.Add(kvp.Key, list);
                    }
                    var r = new RespRet<object>(true, "Ok", dictionary);
                    WriteSystemLog("houses/excute", r, param);
                    return r;
                }
                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("houses/excute", res, param);
                return res;
            }

            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("houses/excute", res, param);
                res.Msg = "系统异常";
                return res;
            }
        }




        [HttpPost("housescj/excuteall")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> ExcuteAll([FromBody] Param param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrWhiteSpace(param.id))
                {
                    return new RespRet<object>(false, "id不能为空");
                }
                if (string.IsNullOrWhiteSpace(param.msgid))
                {
                    return new RespRet<object>(false, "msgid不能为空");
                }
                var sql = _dbContext.ZmSql.FirstOrDefault(x => x.Id == param.id);
                if (sql == null)
                {
                    return new RespRet<object>(false, "未找到可执行语句，请传递正确id");
                }

                var bzsql = _dbContext.ZmSql.FirstOrDefault(x => x.Id == param.msgid);
                if (bzsql == null)
                {
                    return new RespRet<object>(false, "未找到可执行语句，请传递正确msgid");
                }
                object par = new { };
                if (string.IsNullOrWhiteSpace(param.villagedm))
                {
                    sql.Value = sql.Value.Replace("and village=@village", " ");
                    bzsql.Value = bzsql.Value.Replace("and village=@village", " ");
                    sql.Value = sql.Value.Replace("and villagedm=@village", " ");
                    bzsql.Value = bzsql.Value.Replace("and villagedm=@village", " ");
                }
                else
                {
                    par = new { village = param.villagedm };
                }
                if (param.Time != null)
                {
                    if (param.Time.Count == 2)
                    {
                        par = new { village = param.villagedm, start = param.Time[0], end = param.Time[1] };
                    }
                    else
                    {
                        sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                        bzsql.Value = bzsql.Value.Replace("and date(pcsj) between @start and @end", " ");
                    }
                }
                else
                {
                    sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                    bzsql.Value = bzsql.Value.Replace("and date(pcsj) between @start and @end", " ");
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql.Value, par).FirstOrDefault();
                var msgdata = _dbContext.Database.GetDbConnection().Query<object>(bzsql.Value, par).FirstOrDefault();
                Dictionary<string, object> dictionary = new Dictionary<string, object>();
                if (data != null)
                {
                    var properties = data as IDictionary<string, object>;
                    foreach (var kvp in properties)
                    {
                        var stringlist = kvp.Value.ToString().Trim('[', ']').Split(", ");
                        var list = new List<string>();
                        foreach (var item in stringlist)
                        {
                            list.Add(item);
                        }
                        dictionary.Add(kvp.Key, list);
                    }
                    var r = new RespRet<object>(true, "Ok", new { tb = dictionary, msg = msgdata });
                    WriteSystemLog("houses/excute", r, param);
                    return r;
                }
                var res = new RespRet<object>(true, "Ok", new { tb = dictionary, msg = msgdata });
                WriteSystemLog("houses/excute", res, param);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("houses/excute", res, param);
                res.Msg = "系统异常";
                return res;
            }
        }

        private List<string> JsonStr2List(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<List<string>>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }


        private object JsonRegion(string jsonStr)
        {
            try
            {
                return JsonConvert.DeserializeObject<object>(jsonStr);
            }
            catch (System.Exception)
            {
            }

            return null;
        }

        /// <summary>
        /// 统计分析，今日自查情况
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("housescj/tjfx-jrzcqk")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> TjfxJrzcqk([FromBody] Param param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber && c.IsDelete != 1);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                if (RegionCode != param.villagedm)
                {
                    var r1 = new RespRet<object>(false, "请传递正确的村代码");
                    WriteSystemLog("housescj/tjfx-jrzcqk", r1, param);
                    return r1;
                }
                string sql = " select *,(fwzs::int-yzc::int) wzc,round(yzc::numeric*100/fwzs::numeric,2)  zcl  from (select  (select fwds fwzs from zm_building_ledger where villagedm=@villagedm) fwzs,(select count(1) yzc from zm_pc_yh where villagedm=@villagedm) yzc,(select count(case when  date(create_on)=date(now()) then 1 end) jrzc from zm_pc_yh where villagedm=@villagedm )jrzc)a ";


                object par = new { villagedm = param.villagedm };

                if (param.Time != null)
                {
                    if (param.Time.Count == 2)
                    {
                        sql = " select *,(fwzs::int-yzc::int) wzc,round(yzc::numeric*100/fwzs::numeric,2)  zcl  from (select  (select fwds fwzs from zm_building_ledger where villagedm=@villagedm) fwzs,(select count(1) yzc from zm_pc_yh where villagedm=@villagedm )yzc,(select count(case when  date(create_on)=date(now()) then 1 end) jrzc from zm_pc_yh where villagedm=@villagedm)jrzc)a   ";
                        par = new { villagedm = param.villagedm, start = param.Time[0], end = param.Time[1] };
                    }
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, par).FirstOrDefault();
                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("housescj/tjfx-jrzcqk", res, param);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/tjfx-jrzcqk", res, param);
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 市级统计分析，今日自查情况
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("housescj/zctj")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Zctj([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (string.IsNullOrWhiteSpace(usernNumber))
            {
                return new RespRet<object>(false, "用户没有登录");
            }
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                string sql = "";
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;

                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }


                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + param.date[1].Year;

                if (RegionCode.Length == 3 || RegionCode.Length == 6)
                {
                    sql = @" select wzccount,zcount,zccount,yhcount,zgcount,case when value >100 then 100.00||'%' WHEN value IS NULL THEN 0||'%' else value||'%' end zcl,case when value >100 then 100.00 WHEN value IS NULL THEN 0 else value end value   from   (select sum(wzccount) wzccount,sum(zcount) zcount,sum(zccount) zccount,sum(yhcount) yhcount,sum(zgcount) zgcount,round(sum(zccount)*1.00*100/sum(zcount),2)||'%' zcl, round(sum(zccount)*1.00*100/sum(zcount),2) value
from zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename) t1)a   ";
                }
                //if (RegionCode.Length == 6)
                //{
                //    sql = @" select zcount,zccount,yhcount,zgcount,case when value >100 then 100.00||'%' WHEN value IS NULL THEN 0||'%' else value||'%' end zcl,case when value >100 then 100.00 WHEN value IS NULL THEN 0 else value end value   from   (select sum(zcount) zcount,sum(zccount) zccount,sum(yhcount) yhcount,sum(zgcount) zgcount,round(sum(zccount)*1.00*100/sum(zcount),2)||'%' zcl, round(sum(zccount)*1.00*100/sum(zcount),2) value from zcqkgqtext(@startime,@endtime) where xzqdm like @code)a  ";
                //}
                if (RegionCode.Length == 9)
                {
                    sql = @" select wzccount,zcount,zccount,yhcount,zgcount,case when value >100 then 100.00||'%' WHEN value IS NULL THEN 0||'%' else value||'%' end zcl,case when value >100 then 100.00 WHEN value IS NULL THEN 0 else value end value   from   (select sum(wzccount) wzccount,sum(zcount) zcount,sum(zccount) zccount,sum(yhcount) yhcount,sum(zgcount) zgcount,round(sum(zccount)*1.00*100/sum(zcount),2)||'%' zcl, round(sum(zccount)*1.00*100/sum(zcount),2) value from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) where towndm like @code)a
   ";
                }
                if (RegionCode.Length == 12)
                {
                    sql = @" select wzccount,zcount,zccount,yhcount,zgcount,case when value >100 then 100.00||'%' WHEN value IS NULL THEN 0||'%' else value||'%' end zcl,case when value >100 then 100.00 WHEN value IS NULL THEN 0 else value end value   from   (select sum(wzccount) wzccount,sum(zcount) zcount,sum(zccount) zccount,sum(yhcount) yhcount,sum(zgcount) zgcount,round(sum(zccount)*1.00*100/sum(zcount),2)||'%' zcl, round(sum(zccount)*1.00*100/sum(zcount),2) value from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) where villagedm like @code)a
   ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + RegionCode + "%", @startime = param.date[0], @endtime = formattedDate }).FirstOrDefault();
                var res = new RespRet<object>(true, "Ok", data);
                //WriteSystemLog("housescj/zctj", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/zctj", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }



        //        /// <summary>
        //        /// 市级统计分析，今日自查情况
        //        /// </summary>
        //        /// <param name="param"></param>
        //        /// <returns></returns>
        //        [HttpPost("housescj/zcyhtj")]
        //        [Produces("application/json")]
        //        [ProducesResponseType(typeof(RespRet<object>), 200)]
        //        public RespRet<object> zcyhtj([FromBody] DateQuery param)
        //        {
        //            var usernNumber = this.CurrentUser();
        //            if (string.IsNullOrWhiteSpace(usernNumber))
        //            {
        //                return new RespRet<object>(false, "用户没有登录");
        //            }
        //            try
        //            {
        //                if (param == null || param.date == null || param.date.Count != 2)
        //                {
        //                    return new RespRet<object>(false, "请传递时间");
        //                }
        //                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
        //                string sql = "";
        //                var a = JsonRegion(user.District);
        //                JObject obj = JObject.Parse(user.District);
        //                string RegionCode = (string)obj["RegionCode"];
        //                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;


        //                param.date[1] = param.date[1].AddHours(23);
        //                param.date[1] = param.date[1].AddSeconds(59);
        //                param.date[1] = param.date[1].AddMinutes(59);
        //                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
        //                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
        //                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;


        //                if (RegionCode.Length == 3 || RegionCode.Length == 6)
        //                {
        //                    sql = @" select *,( select case when sum(zccount)=0 then 0.00 else(case when round(sum(yhcount)*1.00*100/sum(zccount),2) is null then 0.00 else round(sum(yhcount)*1.00*100/sum(zccount),2) end) end  yhfxl  from zcqkgqtext_fz(@startime,@endtime)) from  (select case when yhcount is null then 0 else yhcount end,zgcount,
        //case when round(zgcount*1.00*100/yhcount,2) is null then 0.00 else round(zgcount*1.00*100/yhcount,2) end yhzgl   from  (select case when  sum(zccount) =0 then null else sum(zccount) end zccount,
        //case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount,
        //case when  sum(zgcount) is null then 0.00 else sum(zgcount) end zgcount from zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename)) a)a  ";
        //                }
        //                //                if (RegionCode.Length == 6)
        //                //                {
        //                //                    sql = @" select case when yhcount is null then 0 else yhcount end,zgcount,
        //                //case when round(yhcount*1.00*100/zccount,2) is null then 0.00 else round(yhcount*1.00*100/zccount,2) end  yhfxl ,
        //                //case when round(zgcount*1.00*100/yhcount,2) is null then 0.00 else round(zgcount*1.00*100/yhcount,2) end yhzgl   from  (select case when  sum(zccount) =0 then null else sum(zccount) end zccount,
        //                //case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount,
        //                //case when  sum(zgcount) is null then 0.00 else sum(zgcount) end zgcount from zcqkgqtext(@startime,@endtime) where xzqdm like @code) a  ";
        //                //                }
        //                if (RegionCode.Length == 9)
        //                {
        //                    sql = @" select *,( select case when sum(zccount)=0 then 0.00 else(case when round(sum(yhcount)*1.00*100/sum(zccount),2) is null then 0.00 else round(sum(yhcount)*1.00*100/sum(zccount),2) end) end yhfxl from zcqkxztext_fz(@startime,@endtime) where towndm like @code) from  (select case when yhcount is null then 0 else yhcount end,zgcount,
        //case when round(zgcount*1.00*100/yhcount,2) is null then 0.00 else round(zgcount*1.00*100/yhcount,2) end yhzgl   from  (select case when  sum(zccount) =0 then null else sum(zccount) end zccount,
        //case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount,
        //case when  sum(zgcount) is null then 0.00 else sum(zgcount) end zgcount from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) where towndm like @code) a)a   ";
        //                }
        //                if (RegionCode.Length == 12)
        //                {
        //                    sql = @" select *,( select case when sum(zccount)=0 then 0.00 else(case when round(sum(yhcount)*1.00*100/sum(zccount),2) is null then 0.00 else round(sum(yhcount)*1.00*100/sum(zccount),2) end) end yhfxl  from zcqkcjtext_fz(@startime,@endtime) where villagedm like @code) from  (select case when yhcount is null then 0 else yhcount end,zgcount,
        //case when round(zgcount*1.00*100/yhcount,2) is null then 0.00 else round(zgcount*1.00*100/yhcount,2) end yhzgl   from  (select case when  sum(zccount) =0 then null else sum(zccount) end zccount,
        //case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount,
        //case when  sum(zgcount) is null then 0.00 else sum(zgcount) end zgcount from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) where villagedm like @code) a)a   ";
        //                }
        //                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @tablename=table_name, @code = "%" + RegionCode + "%", @startime = param.date[0], @endtime = formattedDate }).FirstOrDefault();
        //                var res = new RespRet<object>(true, "Ok", data);
        //                WriteSystemLog("housescj/zcyhtj", res, null);
        //                return res;
        //            }
        //            catch (Exception ex)
        //            {
        //                var res = new RespRet<object>(false, ex.ToString());
        //                WriteSystemLogError("housescj/zcyhtj", res, null);
        //                res.Msg = "系统异常";
        //                return res;
        //            }
        //        }



        /// <summary>
        /// 市级统计分析，今日自查情况
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("housescj/zcyhtj")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> zcyhtj([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (string.IsNullOrWhiteSpace(usernNumber))
            {
                return new RespRet<object>(false, "用户没有登录");
            }
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                string sql = "";
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;


                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }

                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + param.date[1].Year;


                if (RegionCode.Length == 3 || RegionCode.Length == 6)
                {
                    sql = @" select sum(zcount) zcount,
sum(zccount) zccount,
sum(wzccount) wzccount,
case when sum(zcount)=0 then 0  else (case when round(sum(zccount)*100.00/sum(zcount),2)>100 then 100 else round(sum(zccount)*100.00/sum(zcount),2) end)  end  zcl,
sum(yhcount) yhcount,
case when sum(zccount)=0 then 0 else (case when round(sum(yhcount_d)*100.00/sum(zccount),2)>100 then 100 else round(sum(yhcount_d)*100.00/sum(zccount),2) end)  end yhfxl,
sum(zgcount) zgcount,
case when sum(yhcount)=0  then 0 else (case when  round(sum(zgcount)*100.00/sum(yhcount),2)>100 then 100 else round(sum(zgcount)*100.00/sum(yhcount),2) end) end yhzgl
from  zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename) t1; 
  ";
                }
                //                if (RegionCode.Length == 6)
                //                {
                //                    sql = @" select case when yhcount is null then 0 else yhcount end,zgcount,
                //case when round(yhcount*1.00*100/zccount,2) is null then 0.00 else round(yhcount*1.00*100/zccount,2) end  yhfxl ,
                //case when round(zgcount*1.00*100/yhcount,2) is null then 0.00 else round(zgcount*1.00*100/yhcount,2) end yhzgl   from  (select case when  sum(zccount) =0 then null else sum(zccount) end zccount,
                //case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount,
                //case when  sum(zgcount) is null then 0.00 else sum(zgcount) end zgcount from zcqkgqtext(@startime,@endtime) where xzqdm like @code) a  ";
                //                }
                if (RegionCode.Length == 9)
                {
                    sql = @" select sum(zcount) zcount,
sum(zccount) zccount,
sum(wzccount) wzccount,
case when sum(zcount)=0 then 0  else (case when round(sum(zccount)*100.00/sum(zcount),2)>100 then 100 else round(sum(zccount)*100.00/sum(zcount),2) end)  end  zcl,
sum(yhcount) yhcount,
case when sum(zccount)=0 then 0 else (case when round(sum(yhcount_d)*100.00/sum(zccount),2)>100 then 100 else round(sum(yhcount_d)*100.00/sum(zccount),2) end)  end yhfxl,
sum(zgcount) zgcount,
case when sum(yhcount)=0  then 0 else (case when  round(sum(zgcount)*100.00/sum(yhcount),2)>100 then 100 else round(sum(zgcount)*100.00/sum(yhcount),2) end) end yhzgl
from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) t1 where towndm like @code;  ";
                }
                if (RegionCode.Length == 12)
                {
                    sql = @" select sum(zcount) zcount,
sum(zccount) zccount,
sum(wzccount) wzccount,
case when sum(zcount)=0 then 0  else (case when round(sum(zccount)*100.00/sum(zcount),2)>100 then 100 else round(sum(zccount)*100.00/sum(zcount),2) end)  end  zcl,
sum(yhcount) yhcount,
case when sum(zccount)=0 then 0 else (case when round(sum(yhcount_d)*100.00/sum(zccount),2)>100 then 100 else round(sum(yhcount_d)*100.00/sum(zccount),2) end)  end yhfxl,
sum(zgcount) zgcount,
case when sum(yhcount)=0  then 0 else (case when  round(sum(zgcount)*100.00/sum(yhcount),2)>100 then 100 else round(sum(zgcount)*100.00/sum(yhcount),2) end) end yhzgl
from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) t1 where villagedm like @code;    ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @tablename = table_name, @code = "%" + RegionCode + "%", @startime = param.date[0], @endtime = formattedDate }).FirstOrDefault();
                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("housescj/zcyhtj", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/zcyhtj", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }


        /// <summary>
        /// 自查行政区域发现隐患率统计
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("housescj/fxyhltj")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> fxyhltj([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (string.IsNullOrWhiteSpace(usernNumber))
            {
                return new RespRet<object>(false, "用户没有登录");
            }
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                string sql = "";
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                bool isxz = true;
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;


                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }

                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + param.date[1].Year;



                if (param.code.Length == 3 || param.code.Length == 6)
                {
                    sql = @" 
select xzqdm code,xzq name,case when yhcount is null then 0 else yhcount end yhcount,case when zccount is null then 0 else zccount end zccount,
case when round(yhcount*1.00*100/zccount,2) is null then 0 when round(yhcount*1.00*100/zccount,2)>100 then 100 else round(yhcount*1.00*100/zccount,2) end  yhfxl    from  (select
xzqdm,max(xzq) xzq,
case when  sum(zccount) =0 then null else sum(zccount) end zccount,
case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkgqtext_fz(@startime,@endtime::TIMESTAMP,@tablename) GROUP BY xzqdm ) a ORDER BY yhfxl desc,xzqdm  ";
                }
                if (isxz)
                {
                    if (param.code.Length == 6)
                    {
                        sql = @" select towndm code,town name,case when yhcount is null then 0 else yhcount end yhcount,case when zccount is null then 0 else zccount end zccount,
                    case when round(yhcount*1.00*100/zccount,2) is null then 0 when round(yhcount*1.00*100/zccount,2)>100 then 100 else round(yhcount*1.00*100/zccount,2) end  yhfxl    from  (select
                    towndm,max(town) town,
                    case when  sum(zccount) =0 then null else sum(zccount) end zccount,
                    case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkxztext_fz(@startime,@endtime::TIMESTAMP,@yhly,@tablename) where towndm like @code GROUP BY towndm ) a ORDER BY yhfxl desc,towndm  ";
                    }
                }

                if (param.code.Length == 9)
                {
                    sql = @" 
select villagedm code,village name,case when yhcount is null then 0 else yhcount end yhcount,case when zccount is null then 0 else zccount end zccount,
case when round(yhcount*1.00*100/zccount,2) is null then 0 when round(yhcount*1.00*100/zccount,2)>100 then 100 else round(yhcount*1.00*100/zccount,2) end  yhfxl    from  (select
villagedm,max(village) village,
case when  sum(zccount) =0 then null else sum(zccount) end zccount,
case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkcjtext_fz(@startime,@endtime::TIMESTAMP,@yhly,@tablename) where villagedm like @code GROUP BY villagedm ) a ORDER BY yhfxl desc,villagedm   ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select villagedm code,village name,case when yhcount is null then 0 else yhcount end yhcount,case when zccount is null then 0 else zccount end zccount,
case when round(yhcount*1.00*100/zccount,2) is null then 0 when round(yhcount*1.00*100/zccount,2)>100 then 100 else round(yhcount*1.00*100/zccount,2) end  yhfxl    from  (select
villagedm,max(village) village,
case when  sum(zccount) =0 then null else sum(zccount) end zccount,
case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkcjtext_fz(@startime,@endtime::TIMESTAMP,@yhly,@tablename) where villagedm like @code GROUP BY villagedm ) a ORDER BY yhfxl desc,villagedm   ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@yhly="123",@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("housescj/fxyhltj", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/fxyhltj", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }


        /// <summary>
        /// 自查行政区域整改隐患率统计
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost("housescj/zgyhltj")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> zgyhltj([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (string.IsNullOrWhiteSpace(usernNumber))
            {
                return new RespRet<object>(false, "用户没有登录");
            }
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                string sql = "";
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                bool isxz = true;
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }


                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + param.date[1].Year;



                if (param.code.Length == 3 || RegionCode.Length == 6)
                {
                    sql = @" 
select xzqdm code,xzq name,case when yhcount is null then 0 else yhcount end yhcount,case when zgcount is null then 0 else zgcount end zgcount,
case when round(zgcount*1.00*100/yhcount,2) is null then 0 when round(zgcount*1.00*100/yhcount,2)>100 then 100 else round(zgcount*1.00*100/yhcount,2) end  yhzgl    from  (select
xzqdm,max(xzq) xzq,
case when  sum(zgcount) is null then 0 else sum(zgcount) end zgcount,
case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename) GROUP BY xzqdm ) a ORDER BY yhzgl desc,xzqdm  ";
                }
                if (isxz)
                {
                    if (param.code.Length == 6)
                    {
                        sql = @" select towndm code,town name,case when yhcount is null then 0 else yhcount end yhcount,case when zgcount is null then 0 else zgcount end zgcount,
                    case when round(zgcount*1.00*100/yhcount,2) is null then 0 when round(zgcount*1.00*100/yhcount,2)>100 then 100 else round(zgcount*1.00*100/yhcount,2) end  yhzgl    from  (select
                    towndm,max(town) town,
                    case when  sum(zgcount) is null then 0.00 else sum(zgcount) end zgcount,
                    case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) where towndm like @code GROUP BY towndm ) a ORDER BY yhzgl desc,towndm  ";
                    }
                }

                if (param.code.Length == 9)
                {
                    sql = @" 
select villagedm code,village name,case when yhcount is null then 0 else yhcount end yhcount,case when zgcount is null then 0 else zgcount end zgcount,
case when round(zgcount*1.00*100/yhcount,2) is null then 0 when round(zgcount*1.00*100/yhcount,2)>100 then 100 else round(zgcount*1.00*100/yhcount,2) end  yhzgl    from  (select
villagedm,max(village) village,
case when  sum(zgcount) is null then 0 else sum(zgcount) end zgcount,
case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) where villagedm like @code GROUP BY villagedm ) a ORDER BY yhzgl desc,villagedm   ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select villagedm code,village name,case when yhcount is null then 0 else yhcount end yhcount,case when zgcount is null then 0 else zgcount end zgcount,
case when round(zgcount*1.00*100/yhcount,2) is null then 0 when round(zgcount*1.00*100/yhcount,2)>100 then 100 else round(zgcount*1.00*100/yhcount,2) end  yhzgl    from  (select
villagedm,max(village) village,
case when  sum(zgcount) is null then 0 else sum(zgcount) end zgcount,
case when  sum(yhcount) =0 then null else sum(yhcount) end yhcount  from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) where villagedm like @code GROUP BY villagedm ) a ORDER BY yhzgl desc,villagedm   ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("housescj/zgyhltj", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/zgyhltj", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }



        ///// <summary>
        ///// 市级自查饼图
        ///// </summary>
        ///// <returns></returns>

        //[HttpPost("housescj/zcpie")]
        //[Produces("application/json")]
        //[ProducesResponseType(typeof(RespRet<object>), 200)]

        //public RespRet<object> ExcutePie([FromBody] DateQuery param)
        //{
        //    var usernNumber = this.CurrentUser();
        //    var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
        //    try
        //    {

        //        string sql = " ";
        //        if (param == null || param.date == null || param.date.Count != 2)
        //        {
        //            return new RespRet<object>(false, "请传递时间");
        //        }

        //        var a = JsonRegion(user.District);
        //        JObject obj = JObject.Parse(user.District);
        //        string RegionCode = (string)obj["RegionCode"];
        //        RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;

        //        string today = DateTime.Now.ToString("yyyy-MM-dd");
        //        string starttime = Convert.ToDateTime(param.date[0] + "").ToString("yyyy-MM-dd");
        //        string endtime = Convert.ToDateTime(param.date[1] + "").ToString("yyyy-MM-dd");
        //        string basetablename = "";
        //        string yhtablename = "";
        //        string filed = "";
        //        string fileWhere = "";
        //        if (starttime + "" == today && endtime + "" == today)
        //        {
        //            basetablename = "zm_pc_base";
        //            yhtablename = "zm_pc_yh";
        //        }
        //        else
        //        {
        //            string startimemonth = Convert.ToDateTime(param.date[0] + "").ToString("yyyy_MM");
        //            string endmonth = Convert.ToDateTime(param.date[1] + "").ToString("yyyy_MM");
        //            string month = "";
        //            if (startimemonth == endmonth)
        //            {
        //                month = startimemonth;
        //                basetablename = "zm_pc_baselog_" + month; yhtablename = "zm_pc_yhlog_" + month;
        //                filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
        //                fileWhere = " and row_num=1 ";
        //            }
        //            else
        //            {
        //                basetablename = "zm_pc_baselog"; yhtablename = "zm_pc_yhlog";
        //                filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
        //                fileWhere = " and row_num=1 ";
        //            }
        //        }

        //        if (RegionCode.Length == 3 || RegionCode.Length == 6)
        //        {
        //            sql = " select * from  zcqk_gq_pie('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + basetablename + "','"+ yhtablename + "') ";
        //        }

        //        if (RegionCode.Length == 9 )
        //        {
        //            sql = " select * from  zcqk_xz_pie('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + basetablename + "','"+ yhtablename + "','%" + RegionCode + "%') ";
        //        }

        //        if (RegionCode.Length == 12)
        //        {
        //            sql = " select * from  zcqk_cj_pie('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + basetablename + "','"+ yhtablename + "','%" + RegionCode + "%') ";
        //        }


        //        var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + RegionCode + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
        //        var res = new RespRet<object>(true, "Ok", data);
        //        WriteSystemLog("housescj/zcpie", res, null);
        //        return res;
        //    }
        //    catch (Exception ex)
        //    {
        //        var res = new RespRet<object>(false, ex.ToString());
        //        WriteSystemLogError("housescj/zcpie", res, null);
        //        res.Msg = "系统异常";
        //        return res;
        //    }
        //}



        /// <summary>
        /// 市级自查饼图
        /// </summary>
        /// <returns></returns>

        [HttpPost("housescj/zcpie")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> ExcutePie([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            try
            {

                string sql = " ";
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }

                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;

                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }

                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + param.date[1].Year;



                if (RegionCode.Length == 3 || RegionCode.Length == 6)
                {
                    sql = @"  select '已自查' name,case when sum(zcount)=0 then 0  else (case when round(sum(zccount)*100.00/sum(zcount),2)>100 then 100 else round(sum(zccount)*100.00/sum(zcount),2) end)  end value
from zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename) t1   ";
                }
                //if (RegionCode.Length == 6)
                //{
                //    sql = @" select zcount,zccount,yhcount,zgcount,case when value >100 then 100.00||'%' WHEN value IS NULL THEN 0||'%' else value||'%' end zcl,case when value >100 then 100.00 WHEN value IS NULL THEN 0 else value end value   from   (select sum(zcount) zcount,sum(zccount) zccount,sum(yhcount) yhcount,sum(zgcount) zgcount,round(sum(zccount)*1.00*100/sum(zcount),2)||'%' zcl, round(sum(zccount)*1.00*100/sum(zcount),2) value from zcqkgqtext(@startime,@endtime) where xzqdm like @code)a  ";
                //}
                if (RegionCode.Length == 9)
                {
                    sql = @" select '已自查' name, case when sum(zcount)=0 then 0  else (case when round(sum(zccount)*100.00/sum(zcount),2)>100 then 100 else round(sum(zccount)*100.00/sum(zcount),2) end) end value
from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) t1 where towndm like @code ";
                }
                if (RegionCode.Length == 12)
                {
                    sql = @" select '已自查' name, case when sum(zcount)=0 then 0  else (case when round(sum(zccount)*100.00/sum(zcount),2)>100 then 100 else round(sum(zccount)*100.00/sum(zcount),2) end) end value
from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) t1 where villagedm like @code ";
                }
                List<PieDto> res_data = new List<PieDto>();

                var data = _dbContext.Database.GetDbConnection().Query<PieDto>(sql, new {@tablename=table_name, @code = "%" + RegionCode + "%", @startime = param.date[0], @endtime = formattedDate }).FirstOrDefault();
                res_data.Add(data);
                res_data.Add(new PieDto { name = "未自查", value = 100 - data.value });

                var res = new RespRet<object>(true, "Ok", res_data);
                WriteSystemLog("housescj/zcpie", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/zcpie", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }




        [HttpPost("housescj/excutepie")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> ExcutePie([FromBody] Param param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrWhiteSpace(param.id))
                {
                    return new RespRet<object>(false, "id不能为空");
                }
                if (string.IsNullOrWhiteSpace(param.msgid))
                {
                    return new RespRet<object>(false, "msgid不能为空");
                }
                var sql = _dbContext.ZmSql.FirstOrDefault(x => x.Id == param.id);
                if (sql == null)
                {
                    return new RespRet<object>(false, "未找到可执行语句，请传递正确id");
                }

                var bzsql = _dbContext.ZmSql.FirstOrDefault(x => x.Id == param.msgid);
                if (bzsql == null)
                {
                    return new RespRet<object>(false, "未找到可执行语句，请传递正确msgid");
                }
                object par = new { };
                if (string.IsNullOrWhiteSpace(param.villagedm))
                {
                    sql.Value = sql.Value.Replace("and village=@village", " ");
                    bzsql.Value = bzsql.Value.Replace("and village=@village", " ");
                    sql.Value = sql.Value.Replace("and villagedm=@village", " ");
                    bzsql.Value = bzsql.Value.Replace("and villagedm=@village", " ");
                }
                else
                {
                    par = new { village = param.villagedm };
                }
                if (param.Time != null)
                {
                    if (param.Time.Count == 2)
                    {
                        par = new { village = param.villagedm, start = param.Time[0], end = param.Time[1] };
                    }
                    else
                    {
                        sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                        bzsql.Value = bzsql.Value.Replace("and date(pcsj) between @start and @end", " ");
                    }
                }
                else
                {
                    sql.Value = sql.Value.Replace("and date(pcsj) between @start and @end", " ");
                    bzsql.Value = bzsql.Value.Replace("and date(pcsj) between @start and @end", " ");
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql.Value, par).ToList();
                var msgdata = _dbContext.Database.GetDbConnection().Query<object>(bzsql.Value, par).FirstOrDefault();
                var res = new RespRet<object>(true, "Ok", new { tb = data, msg = msgdata });
                WriteSystemLog("houses/excutepie", res, param);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("houses/excutepie", res, param);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 已自查为自查饼状图二级页面，按率倒序排
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/zclbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> Zclbar([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                string sql = "  ";
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                bool isxz = true;
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }


                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + param.date[1].Year;


                if (param.code.Length == 3 || param.code.Length == 6)
                {
                    sql = " select *from zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename) t1 ORDER BY  zcl desc,xzqdm;  ";
                }
                if (param.code.Length == 6 && isxz)
                {
                    sql = " select *from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) t1 where towndm like @code ORDER BY  zcl desc,towndm;  ";
                }

                    
                if (param.code.Length == 9 || (param.code.Length == 12))
                {
                    sql = " select *from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) t1 where villagedm like @code ORDER BY  zcl desc,villagedm;  ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate }).ToList();

                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("housescj/zclbar", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/zclbar", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }







        /// <summary>
        /// 自查统计-为自查房屋下钻
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/wzcfw")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> wzcfw([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                string sql = "";
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                bool isxz = true;
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;


                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }

                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddSeconds(59);
                param.date[1] = param.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                if (param.code.Length == 3 || param.code.Length == 6)
                {
                    sql = @" select  xzqdm code,xzq name,wzccount from zcqkgqtext(@startime,@endtime::TIMESTAMP,@tablename) ORDER BY  xzqdm   
  ";
                }
                if (isxz)
                {
                    if (param.code.Length == 6)
                    {
                        sql = @" 
 select towndm code,town name,wzccount from zcqkxztext(@startime,@endtime::TIMESTAMP,@tablename) where towndm like @code ORDER BY  towndm  
    ";
                    }
                }
                if (param.code.Length == 9 || (param.code.Length == 12))
                {
                    sql = @" select villagedm code,village name,wzccount from zcqkcjtext(@startime,@endtime::TIMESTAMP,@tablename) where villagedm like @code ORDER BY  villagedm    ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate }).ToList();

                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("housescj/wzcfw", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/wzcfw", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 发现整改隐患 统计 详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/yhdetaile")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<FwlbDto>>), 200)]
        public RespRet<Page<object>> Yhdetailes([FromBody] YhQuery pageRequest)
        {
            if (pageRequest == null)
            {
                return new RespRet<Page<object>>(false, "请传递参数");
            }
            if (pageRequest.PageIndex < 0)
            {
                return new RespRet<Page<object>>(false, "PageIndex must >= 0");
            }
            var userNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == userNumber && c.IsDelete != 1 && c.Disabled != 1);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<object>>(false, "user not logined");
            }
            try
            {
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                sbSql.Append($@" select fwbh,create_on,check_nickname,check_name,id  from  zm_pc_yhlog_dx  where create_on between @startime and @endtime and villagedm like @code    ");
                var CountSql = new StringBuilder();
                CountSql.Append($@" select count(1)  from  zm_pc_yhlog_dx  where create_on between @startime and @endtime and villagedm like @code   ");
                if (string.IsNullOrWhiteSpace(pageRequest.Code))
                {
                    return new RespRet<Page<object>>(false, "行政区划代码不能为空");
                }
                if (pageRequest.Date == null || pageRequest.Date.Count != 2)
                {
                    return new RespRet<Page<object>>(false, "时间不能为空");
                }
                if (pageRequest.Type == 2)
                {
                    sbSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();
                    sbSql.Append($@" select fwbh,create_on,check_nickname,check_name,id  from  zm_pc_yhlog_dx  where create_on between @startime and @endtime and villagedm like @code and zg_value='1'   ");
                    CountSql = new StringBuilder();
                    CountSql.Append($@" select count(1)  from  zm_pc_yhlog_dx  where create_on between @startime and @endtime and villagedm like @code  and zg_value='1' ");
                }
                sbSql.Append($@" order by fwbh");
                sbSql.Append($@" limit @limit offset @offset");
                var datas = _dbContext.Database.GetDbConnection().Query<object>(sbSql.ToString(), new { @code = "%" + pageRequest.Code + "%", @startime = pageRequest.Date[0], @endtime = pageRequest.Date[1], @limit = pageRequest.Limit, @offset = pageRequest.Limit * pageRequest.PageIndex }).ToList();
                var count = _dbContext.Database.GetDbConnection().Query<int>(CountSql.ToString(), new { @code = "%" + pageRequest.Code + "%", @startime = pageRequest.Date[0], @endtime = pageRequest.Date[1], @limit = pageRequest.Limit, @offset = pageRequest.Limit * pageRequest.PageIndex }).FirstOrDefault();
                var pageData = new RespRet<Page<object>>(true, "ok", new Page<object>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count)); WriteSystemLog("housescj/yhdetaile", pageData, pageRequest);

                return pageData;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<object>>(false, ex.ToString());
                WriteSystemLogError("housescj/yhdetaile", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }








        /// <summary>
        /// 市级 获取自查率 各区县的总数量  自查数量  隐患数量  整改数量
        /// </summary>
        /// <param name="pageRequest"></param>         
        /// <returns></returns>
        [HttpGet("housescj/checkrategq")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<object>>), 200)]
        public RespRet<object> HouseMngCheckQ()
        {
            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;

            string sql = @" select *,replace(zcl, '%', '')::numeric value,xzq name,xzqdm code  from zcqkgqview  ";
            var list = _dbContext.Database.GetDbConnection().Query<object>(sql).ToList();
            return new RespRet<object>(true, "Ok", list);
        }


        /// <summary>
        /// 市级 获取自查率 各乡镇的总数量  自查数量 自查整改数量  自查隐患数量等
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housescj/checkrategxz")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> HouseMngCheckxz([FromQuery] string code)
        {

            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(code))
            {
                code = RegionCode;
            }
            code = code.Substring(code.Length - 3) == "000" ? code.Substring(0, code.Length - 3) : code;


            string sql = @" select *,replace(zcl, '%', '')::numeric value,town name,towndm code  from zcqkxzview where towndm like @code	";

            var list = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + code + "%" }).ToList();
            int count = list.Count;
            return new RespRet<object>(true, "Ok", list);
        }


        /// <summary>
        /// 市级 获取自查率 各村的总数量  自查数量 自查整改数量  自查隐患数量等
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housescj/checkrategcj")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> HouseMngCheckcj([FromQuery] string code)
        {

            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(code))
            {
                code = RegionCode;
            }
            code = code.Substring(code.Length - 3) == "000" ? code.Substring(0, code.Length - 3) : code;


            string sql = @" select *,replace(zcl, '%', '')::numeric value,village name,villagedm code  from zcqkcjview where villagedm like @code	";

            var list = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + code + "%" }).ToList();
            return new RespRet<object>(true, "Ok", list);
        }


        /// <summary>
        /// 市级-人员疏散通道
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/rysstd")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Rysstd([FromBody] DateQuery param)
        {

            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            string sql = @" ";
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string starttime = Convert.ToDateTime(param.date[0] + "").ToString("yyyy-MM-dd");
            string endtime = Convert.ToDateTime(param.date[1] + "").ToString("yyyy-MM-dd");
            string tablename = "";
            string filed = "";
            string fileWhere = "";
            string table_name = "zm_building_fwzt_" + param.date[1].Year;

            if (endtime + "" == today)
            {
                tablename = "zm_pc_base";
            }
            else
            {
                string startimemonth = Convert.ToDateTime(param.date[0] + "").ToString("yyyy_MM");
                string endmonth = Convert.ToDateTime(param.date[1] + "").ToString("yyyy_MM");
                string month = "";
                if (startimemonth == endmonth)
                {
                    month = startimemonth;
                    tablename = "zm_pc_baselog_" + month;
                    filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                    fileWhere = " and row_num=1 ";
                }
                else
                {
                    tablename = "zm_pc_baselog";
                    filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                    fileWhere = " and row_num=1 ";
                }
            }

            if (param.code.Length == 3 || param.code.Length == 6)
            {
                sql = " select * from  rysstd_gq_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','"+table_name+"') ";
            }

            if (param.code.Length == 9 )
            {
                sql = " select * from  rysstd_xz_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+table_name+"') ";
            }
            if (param.code.Length == 12)
            {
                sql = " select * from  rysstd_cj_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+table_name+"') ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<RysstdDto>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.dbl, name = "已达标" });
            tb.Add(new { value = 100 - msg.dbl, name = "未达标" });
            return new RespRet<object>(true, "Ok", new { msg = msg, tb = tb });
        }




        /// <summary>
        /// 获取隐患详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/yhxq")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcYhLogDxDtoXq>), 200)]
        public RespRet<ZcJcYhDetail> Yhxq([FromBody] YhDetaile param)
        {
            var userNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<ZcJcYhDetail>(false, "user not logined");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append($@"select id Id,fwbh Fwbh,jcly Jcly,check_id CheckId,check_name CheckName,check_sm CheckSm,check_zp CheckZp,zg_value ZgValue  FROM zm_pc_yhlog_dx  where id=@Id  ");

                if (param.Type == 2)
                {
                    sbSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append($@"select id Id,fwbh Fwbh,jcly Jcly,check_id CheckId,check_name CheckName,check_sm CheckSm,check_zp CheckZp,zg_value ZgValue  FROM zm_pc_yhlog_dx_cj where id=@Id    ");
                }
                var from = _dbContext.Database.GetDbConnection().Query<ZcJcYhDetail>(sbSql.ToString(), new { Id = param.Id }).FirstOrDefault();
                if (from == null)
                {
                    var res = new RespRet<ZcJcYhDetail>(false, "未找到隐患信息，请传递正确ID", from);
                    WriteSystemLog("housescj/yhxq", res, param);
                    return res;
                }
                from.CheckZp = from.CheckZp == null ? null : string.Join("|", JsonStr2List(from.CheckZp));
                //from.IsHaveYh = from.ZgValue == 0 ? "是" : "否";
                if (from.Jcly == "6")
                {
                    from.Jcly = "村民自查";
                }
                if (from.Jcly == "5")
                {
                    from.Jcly = "村级检查";
                }
                if (from.Jcly == "4")
                {
                    from.Jcly = "街镇检查";
                }
                if (from.Jcly == "3")
                {
                    from.Jcly = "区级核查";
                }
                if (from.Jcly == "2")
                {
                    from.Jcly = "市级核查";
                }
                var r = new RespRet<ZcJcYhDetail>(true, "Ok", from);
                WriteSystemLog("housescj/yhxq", r, param);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<ZcJcYhDetail>(false, ex.ToString());
                WriteSystemLogError("housescj/yhxq", res, param);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 获取隐患整改详情
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/yhzgxq")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<ZmPcYhLogDxDtoXq>), 200)]
        public RespRet<ZcJcZgYhDetail> YhZgxq([FromBody] YhDetaile param)
        {
            var userNumber = this.CurrentUser();
            try
            {
                if (string.IsNullOrEmpty(userNumber))
                {
                    return new RespRet<ZcJcZgYhDetail>(false, "user not logined");
                }
                if (string.IsNullOrEmpty(param.Id))
                {
                    return new RespRet<ZcJcZgYhDetail>(false, "id不能为空");
                }
                var sbSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();

                sbSql.Append($@" select a.id Id,jcly Jcly,a.fwbh Fwbh,a.check_id CheckId,a.check_name CheckName,check_zp CheckZp,b.zg_value ZgValue,zg_zp ZgZp,a.zg_yhsyq_num ZgYhsyqNum,a.zg_ddc_num ZgDdcNum,check_sm CheckSm  FROM zm_pc_yhlog_dxzgson a,zm_pc_yhlog_dx b where a.yhlog_dx_id=b.id    ");
                sbSql.Append($@" and a.id = @Id ");

                if (param.Type == 2)
                {
                    sbSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSql.Append($@" select a.id Id,jcly Jcly,a.fwbh Fwbh,a.check_id CheckId,a.check_name CheckName,check_zp CheckZp,b.zg_value ZgValue,zg_zp ZgZp,a.zg_yhsyq_num ZgYhsyqNum,a.zg_ddc_num ZgDdcNum,check_sm CheckSm  FROM zm_pc_yhlog_dxzgson_cj a,zm_pc_yhlog_dx_cj b where a.yhlog_dx_id=b.id    ");
                }
                var from = _dbContext.Database.GetDbConnection().Query<ZcJcZgYhDetail>(sbSql.ToString(), new { Id = param.Id }).FirstOrDefault();
                if (from == null)
                {
                    var res = new RespRet<ZcJcZgYhDetail>(false, "未找到隐患信息，请传递正确ID", from);
                    WriteSystemLog("housescj/yhzgxq", res, param);
                    return res;
                }
                from.CheckZp = from.CheckZp == null ? null : string.Join("|", JsonStr2List(from.CheckZp));
                from.ZgZp = from.ZgZp == null ? null : string.Join("|", JsonStr2List(from.ZgZp));
                if (from.Jcly == "6")
                {
                    from.Jcly = "村民自查";
                }
                if (from.Jcly == "5")
                {
                    from.Jcly = "村级检查";
                }
                if (from.Jcly == "4")
                {
                    from.Jcly = "街镇检查";
                }
                if (from.Jcly == "3")
                {
                    from.Jcly = "区级核查";
                }
                if (from.Jcly == "2")
                {
                    from.Jcly = "市级核查";
                }

                var r = new RespRet<ZcJcZgYhDetail>(true, "Ok", from);
                WriteSystemLog("housescj/yhzgxq", r, param);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<ZcJcZgYhDetail>(false, ex.ToString());
                WriteSystemLogError("housescj/yhzgxq", res, param);
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 已达标未达标饼状图二级页面，按达标率倒序排
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/dblbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> Dblbar([FromBody] DateQuery param)
        {
            try
            {
                var usernNumber = this.CurrentUser();
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                var isxz = true;
                if (param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "时间不能为空");
                }


                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }
                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddMinutes(59);
                param.date[1] = param.date[1].AddSeconds(59);
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

                string sql = @" ";
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                string starttime = Convert.ToDateTime(param.date[0] + "").ToString("yyyy-MM-dd");
                string endtime = Convert.ToDateTime(param.date[1] + "").ToString("yyyy-MM-dd");
                string tablename = "";
                string filed = "";
                string fileWhere = "";
                string table_name = "zm_building_fwzt_" + param.date[1].Year;
                if (endtime + "" == today)
                {
                    tablename = "zm_pc_base";
                }
                else
                {
                    string startimemonth = Convert.ToDateTime(param.date[0] + "").ToString("yyyy_MM");
                    string endmonth = Convert.ToDateTime(param.date[1] + "").ToString("yyyy_MM");
                    string month = "";
                    if (startimemonth == endmonth)
                    {
                        month = startimemonth;
                        tablename = "zm_pc_baselog_" + month;
                        filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                        fileWhere = " and row_num=1 ";
                    }
                    else
                    {
                        tablename = "zm_pc_baselog";
                        filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                        fileWhere = " and row_num=1 ";
                    }
                }

                if (param.code.Length == 3 || param.code.Length == 6)
                {
                    sql = " select * from  dblbar_gq_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','"+ table_name + "') ";
                }

                if (param.code.Length == 6 && isxz)
                {
                    sql = " select * from  dblbar_xz_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+ table_name + "') ";
                }

                if (param.code.Length == 9 || param.code.Length == 12)
                {
                    sql = " select * from  dblbar_cj_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+ table_name + "') ";
                }


                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime =endtime }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
                WriteSystemLog("houses/dblbar", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("houses/dblbar", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        ///人员疏散通道二级页面-人员疏散通道建设（正在施工、改造完成、正在制定方案）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housescj/rysstdjspie")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> RysstdJs([FromQuery] string code)
        {
            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(code))
            {
                code = RegionCode;
            }
            code = code.Substring(code.Length - 3) == "000" ? code.Substring(0, code.Length - 3) : code;
            string tbsql = @$" ";
            string sql = @" ";
            if (code.Length == 3)
            {
                tbsql = @$" 	select  sum(sstdywc) value, '改造完成' name from jbxxgqview  
					 union all
				select  sum(zzsggz) value , '正在施工' name  from jbxxgqview 
				   union all
				select  sum(zzzdfa) value , '制定方案' name from jbxxgqview 	  ";
                sql = @" select  sum(sstdywc) sstdywc,sum(zzsggz) zzsggz,sum(zzzdfa) zzzdfa from jbxxgqview  ";
            }
            if (code.Length == 6)
            {
                tbsql = @$"  select  sum(sstdywc) value, '改造完成' name from jbxxgqview  where xzqdm like @code
					 union all
				select  sum(zzsggz) value , '正在施工' name  from jbxxgqview where xzqdm like @code
				   union all
				select  sum(zzzdfa) value , '制定方案' name from jbxxgqview 	 where xzqdm like @code
 	  ";
                sql = @" select  sum(sstdywc) sstdywc,sum(zzsggz) zzsggz,sum(zzzdfa) zzzdfa from jbxxgqview where xzqdm like @code  ";
            }

            if (code.Length == 9)
            {
                tbsql = @$"  select  sum(sstdywc) value, '改造完成' name from jbxxxzview  where towndm like @code
					 union all
				select  sum(zzsggz) value , '正在施工' name  from jbxxxzview where towndm like @code
				   union all
				select  sum(zzzdfa) value , '制定方案' name from jbxxxzview 	 where towndm like @code
 	  ";
                sql = @" select  sum(sstdywc) sstdywc,sum(zzsggz) zzsggz,sum(zzzdfa) zzzdfa from jbxxxzview where towndm like @code  ";

            }
            if (code.Length == 12)
            {
                tbsql = @$"  select  sum(sstdywc) value, '改造完成' name from jbxxcjview  where villagedm like @code
					 union all
				select  sum(zzsggz) value , '正在施工' name  from jbxxcjview where villagedm like @code
				   union all
				select  sum(zzzdfa) value , '制定方案' name from jbxxcjview 	 where villagedm like @code
 	  ";
                sql = @" select  sum(sstdywc) sstdywc,sum(zzsggz) zzsggz,sum(zzzdfa) zzzdfa from jbxxcjview where villagedm like @code  ";
            }

            var tb = _dbContext.Database.GetDbConnection().Query<object>(tbsql, new { @code = "%" + code + "%" }).ToList();
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + code + "%" }).FirstOrDefault();
            return new RespRet<object>(true, "Ok", new { tb = tb, msg = msg });
        }



        /// <summary>
        ///人员疏散通道二级页面-人员疏散通道建设（已完成）（增设疏散楼梯，利用相邻相近建筑疏散）
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housescj/rysstdjswcpie")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> rysstdjswcpie([FromQuery] string code)
        {

            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(code))
            {
                code = RegionCode;
            }
            code = code.Substring(code.Length - 3) == "000" ? code.Substring(0, code.Length - 3) : code;

            string tbsql = @$" ";
            string sql = @" ";
            if (code.Length == 3)
            {
                tbsql = @$" 	select  sum(jzswhntjzssslt) value, '建筑室外或内天井增设疏散楼梯' name from jbxxgqview  
					 union all
				select  sum(jzsnzssslt) value , '建筑室内增设疏散楼梯' name  from jbxxgqview 
				   union all
				select  sum(lyxlhxjjzss) value , '利用相邻或相近建筑疏散' name from jbxxgqview	  ";
                sql = @"  select  sum(jzswhntjzssslt) jzswhntjzssslt,sum(jzsnzssslt) jzsnzssslt,sum(lyxlhxjjzss) lyxlhxjjzss from jbxxgqview  ";
            }
            if (code.Length == 6)
            {
                tbsql = @$"  select  sum(jzswhntjzssslt) value, '建筑室外或内天井增设疏散楼梯' name from jbxxgqview  where xzqdm like @code
					 union all
				select  sum(jzsnzssslt) value , '建筑室内增设疏散楼梯' name  from jbxxgqview where xzqdm like @code
				   union all
				select  sum(lyxlhxjjzss) value , '利用相邻或相近建筑疏散' name from jbxxgqview where xzqdm like @code
 	  ";
                sql = @" select  sum(jzswhntjzssslt) jzswhntjzssslt,sum(jzsnzssslt) jzsnzssslt,sum(lyxlhxjjzss) lyxlhxjjzss from jbxxgqview where xzqdm like @code  ";
            }

            if (code.Length == 9)
            {
                tbsql = @$"  select  sum(jzswhntjzssslt) value, '建筑室外或内天井增设疏散楼梯' name from jbxxxzview   where towndm like @code
					 union all
				select  sum(jzsnzssslt) value , '建筑室内增设疏散楼梯' name  from jbxxxzview  where towndm like @code
				   union all
				select  sum(lyxlhxjjzss) value , '利用相邻或相近建筑疏散' name from jbxxxzview where towndm like @code
 	  ";
                sql = @" select  sum(jzswhntjzssslt) jzswhntjzssslt,sum(jzsnzssslt) jzsnzssslt,sum(lyxlhxjjzss) lyxlhxjjzss from jbxxxzview where towndm like @code  ";

            }
            if (code.Length == 12)
            {
                tbsql = @$"  select  sum(jzswhntjzssslt) value, '建筑室外或内天井增设疏散楼梯' name from jbxxcjview   where villagedm like @code
					 union all
				select  sum(jzsnzssslt) value , '建筑室内增设疏散楼梯' name  from jbxxcjview  where villagedm like @code
				   union all
				select  sum(lyxlhxjjzss) value , '利用相邻或相近建筑疏散' name from jbxxcjview where villagedm like @code
 	  ";
                sql = @" select  sum(jzswhntjzssslt) jzswhntjzssslt,sum(jzsnzssslt) jzsnzssslt,sum(lyxlhxjjzss) lyxlhxjjzss from jbxxcjview where villagedm like @code  ";
            }

            var tb = _dbContext.Database.GetDbConnection().Query<object>(tbsql, new { @code = "%" + code + "%" }).ToList();
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + code + "%" }).FirstOrDefault();
            return new RespRet<object>(true, "Ok", new { tb = tb, msg = msg });
        }

   //     /// <summary>
   //     /// 窗口护栏拆除zhuzhuangtu
   //     /// </summary>
   //     /// <param name="pageRequest"></param>
   //     /// <returns></returns>
   //     [HttpPost("housescj/ckhlccbar")]
   //     [Produces("application/json")]
   //     [ProducesResponseType(typeof(RespRet<object>), 200)]
   //     public RespRet<object> ckhlccbar([FromBody] DateQuery param)
   //     {

   //         var usernNumber = this.CurrentUser();
   //         if (param.date == null || param.date.Count() != 2)
   //         {
   //             return new RespRet<object>(false, "时间不能为空");
   //         }
   //         param.date[1] = param.date[1].AddHours(23);
   //         param.date[1] = param.date[1].AddMinutes(59);
   //         param.date[1] = param.date[1].AddSeconds(59);
   //         var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
   //         var a = JsonRegion(user.District);
   //         JObject obj = JObject.Parse(user.District);
   //         string RegionCode = (string)obj["RegionCode"];
   //         RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
   //         var isxz = true;
   //         if (string.IsNullOrWhiteSpace(param.code))
   //         {
   //             isxz = false;
   //             param.code = RegionCode;
   //         }
   //         param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

   //         string sql = @" ";
   //         if (param.code.Length == 3 || param.code.Length == 6)
   //         {
   //             sql = @" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (	select  zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select sum(zcount)zcount,xzqdm code,max(xzq) name, case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxgqview GROUP BY xzqdm) a left join (select xzqdm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
			//		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
   //) a   group by xzqdm)  b	 on a.code=b.xzqdm)a ORDER BY ccl desc,code  
   //       ";
   //         }
   //         if (param.code.Length == 6 && isxz)
   //         {
   //             sql = @" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl  ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
			//		select   zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,towndm code,max(town) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxxzview where towndm like @code group by towndm) a left join  (select towndm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
			//		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
   //) a   group by towndm )  b on a.code=b.towndm )a ORDER BY ccl desc,code	";
   //         }

   //         if (param.code.Length == 9)
   //         {
   //             sql = @" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
			//		select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code  ";
   //         }
   //         if (param.code.Length == 12)
   //         {
   //             sql = @" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
			//		select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code  ";
   //         }
   //         var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
   //         return new RespRet<object>(true, "Ok", data);
   //     }


   //     /// <summary>
   //     /// 窗口护栏拆除
   //     /// </summary>
   //     /// <param name="pageRequest"></param>
   //     /// <returns></returns>
   //     [HttpPost("housescj/ckhlccmsg")]
   //     [Produces("application/json")]
   //     [ProducesResponseType(typeof(RespRet<object>), 200)]
   //     public RespRet<object> ckhlccmsg([FromBody] DateQuery param)
   //     {
   //         var usernNumber = this.CurrentUser();
   //         if (param.date == null || param.date.Count != 2)
   //         {
   //             return new RespRet<object>(false, "时间不能为空");
   //         }
   //         param.date[1] = param.date[1].AddHours(23);
   //         param.date[1] = param.date[1].AddMinutes(59);
   //         param.date[1] = param.date[1].AddSeconds(59);
   //         var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
   //         var a = JsonRegion(user.District);
   //         JObject obj = JObject.Parse(user.District);
   //         string RegionCode = (string)obj["RegionCode"];
   //         RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
   //         if (string.IsNullOrWhiteSpace(param.code))
   //         {
   //             param.code = RegionCode;
   //         }
   //         param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

   //         string sql = @" ";
   //         if (param.code.Length == 3 || param.code.Length == 6)
   //         {
   //             sql = @" 	 select sum(zcount) ckfhwggptzlsl,sum(ccfhwggptzlsl)ccfhwggptzlsl,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/sum(zcount),0.00),2)  ccl from (  select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (	select  zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select sum(zcount)zcount,xzqdm code,max(xzq) name, case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxgqview GROUP BY xzqdm) a left join (select xzqdm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
			//		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
   //) a   group by xzqdm)  b	 on a.code=b.xzqdm)a ORDER BY ccl desc,code) a ";
   //         }
   //         if (param.code.Length == 9)
   //         {
   //             sql = @" 	select sum(zcount) ckfhwggptzlsl,sum(ccfhwggptzlsl)ccfhwggptzlsl,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/sum(zcount),0.00),2)  ccl from( select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl  ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
			//		select   zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,towndm code,max(town) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxxzview where towndm like @code group by towndm) a left join  (select towndm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
			//		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
   //) a   group by towndm )  b on a.code=b.towndm )a ORDER BY ccl desc,code)a ";
   //         }
   //         if (param.code.Length == 12)
   //         {
   //             sql = @"  select sum(zcount) ckfhwggptzlsl,sum(ccfhwggptzlsl)ccfhwggptzlsl,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/sum(zcount),0.00),2)  ccl from (select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
			//		select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			//case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code )a	"; }
   //         var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).FirstOrDefault();
   //         return new RespRet<object>(true, "Ok", msg);
        //}



        /// <summary>
        /// 检查记录返回房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/searchcj-dbfw")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<ZmPcYhLogCjDto2>>), 200)]
        public RespRet<Page<ZmPcYhLogCjDto>> SearchAttrList2([FromBody] FwlbTj pageRequest)
        {

            var userNumber = this.CurrentUser();
            if (string.IsNullOrEmpty(userNumber))
            {
                return new RespRet<Page<ZmPcYhLogCjDto>>(false, "user not logined");
            }
            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.isdb != 1 && pageRequest.isdb != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择正确类型");
                }

                if (pageRequest.date == null || pageRequest.date.Count != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "请选择时间");
                }
                pageRequest.date[1] = pageRequest.date[1].AddHours(23);
                pageRequest.date[1] = pageRequest.date[1].AddMinutes(59);
                pageRequest.date[1] = pageRequest.date[1].AddSeconds(59);

                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();

                sbSql.Append(@$" select address,hzxm,fwbh from  zm_buildings where fwbh in( SELECT 
          DISTINCT CASE
                  when tdsfdb='是' then fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,tdsfdb, t1.fwbh
             FROM zm_pc_baselog t1 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE t1.create_on BETWEEN @startime AND @endtime AND t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') ) ");
                sbCountSql.Append(@$" select count(1) count from  zm_buildings where fwbh in( SELECT 
          
               DISTINCT CASE
                  when tdsfdb='是' then fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,tdsfdb, t1.fwbh
             FROM zm_pc_baselog t1 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE t1.create_on BETWEEN @startime AND @endtime AND t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') )  ");
                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @startime = pageRequest.date[0], @endtime = pageRequest.date[1] }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                sqlParams.Add(new NpgsqlParameter("startime", pageRequest.date[0]));
                sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.date[1]));
                if (pageRequest.isdb != 1)
                {
                    sbSql = new StringBuilder();
                    sbCountSql = new StringBuilder();
                    sbSqlWhere = new StringBuilder();
                    sbSqlWhere2 = new StringBuilder();
                    sqlParams = new List<NpgsqlParameter>();

                    sbSql.Append(@$" select address,hzxm,fwbh from  zm_buildings where fwbh in( SELECT 
          DISTINCT CASE
                  when tdsfdb='否' then fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,tdsfdb, t1.fwbh
             FROM zm_pc_baselog t1 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE t1.create_on BETWEEN @startime AND @endtime AND t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') ) ");
                    sbCountSql.Append(@$" select count(1) count from  zm_buildings where fwbh in( SELECT 
          
               DISTINCT CASE
                  when tdsfdb='否' then fwbh
                END
        FROM (
            SELECT ROW_NUMBER() OVER (PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,t1.create_on,t1.village,tdsfdb, t1.fwbh
             FROM zm_pc_baselog t1 
     LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh 
     JOIN zm_buildings t5 ON t1.fwbh = t5.fwbh 
            WHERE t1.create_on BETWEEN @startime AND @endtime AND t2.fwzt IS NULL and t1.village=@code 
        ) AS zm_pc_base  where row_num =1
       and village=@code and fwbh not in (select fwbh From zm_pc_base_cj where fwzt is not null) and fwbh not in (select fwbh From zm_pc_base_sqjz where fwzt is not null and yhly='4') ) ");
                    count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @startime = pageRequest.date[0], @endtime = pageRequest.date[1] }).FirstOrDefault();
                    sbSql.Append($@" order by fwbh ");
                    sbSql.Append($@" limit @limit offset @offset");
                    sqlParams.Add(new NpgsqlParameter("code", pageRequest.code));
                    sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                    sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                    sqlParams.Add(new NpgsqlParameter("startime", pageRequest.date[0]));
                    sqlParams.Add(new NpgsqlParameter("endtime", pageRequest.date[1]));
                }

                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                //WriteSystemLog("housescj/search-attryhzclog", r, pageRequest);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-attryhzclog", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 检查记录返回房屋
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost("housescj/wzcfwdetail")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<Page<ZmPcYhLogCjDto2>>), 200)]
        public RespRet<Page<ZmPcYhLogCjDto>> wzcfwdetail([FromBody] FwlbTj pageRequest)
        {

            //var userNumber = this.CurrentUser();
            //if (string.IsNullOrEmpty(userNumber))
            //{
            //    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "user not logined");
            //}
            try
            {
                if (string.IsNullOrWhiteSpace(pageRequest.code))
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "村代码不能为空");
                }
                if (pageRequest.date == null || pageRequest.date.Count != 2)
                {
                    return new RespRet<Page<ZmPcYhLogCjDto>>(false, "时间不能为空");
                }
                var sbSql = new StringBuilder();
                var sbCountSql = new StringBuilder();
                var sbSqlWhere = new StringBuilder();
                var sbSqlWhere2 = new StringBuilder();
                var sqlParams = new List<NpgsqlParameter>();
                pageRequest.code = pageRequest.code.Substring(pageRequest.code.Length - 3) == "000" ? pageRequest.code.Substring(0, pageRequest.code.Length - 3) : pageRequest.code;


                if (pageRequest.date[1] > DateTime.Now)
                {
                    pageRequest.date[1] = DateTime.Today;
                }
                pageRequest.date[1] = pageRequest.date[1].AddHours(23);
                pageRequest.date[1] = pageRequest.date[1].AddSeconds(59);
                pageRequest.date[1] = pageRequest.date[1].AddMinutes(59);
                // 假设 param.date[1] 已经被设置为 2025-06-14 23:59:59
                string formattedDate = pageRequest.date[1].ToString("yyyy-MM-dd HH:mm:ss");
                string table_name = "zm_building_fwzt_" + DateTime.Now.Year;

                sbSql.Append(@$" 
select fwbh,address,hzxm From zm_buildings  where fwbh not in ( select distinct fwbh From  zm_pc_yhlog where  create_on between @startime and @endtime::TIMESTAMP and fwbh not in (select fwbh from {table_name} where final_create_on=@endtime and final_fwzt is not null)) 
and fwbh not in (select fwbh from {table_name} where final_create_on=@endtime and final_fwzt is not null)and villagedm=@code
 ");
                sbCountSql.Append(@$" 
select count(1) From zm_buildings  where fwbh not in ( select distinct fwbh From  zm_pc_yhlog where  create_on between @startime and @endtime::TIMESTAMP and fwbh not in (select fwbh from {table_name} where final_create_on=@endtime and final_fwzt is not null)) 
and fwbh not in (select fwbh from {table_name} where final_create_on=@endtime and final_fwzt is not null)and villagedm=@code
 ");
                var count = _dbContext.Database.GetDbConnection().Query<int>(sbCountSql.ToString(), new { @code = pageRequest.code, @startime = pageRequest.date[0], @endtime = formattedDate }).FirstOrDefault();
                sbSql.Append($@" order by fwbh ");
                sbSql.Append($@" limit @limit offset @offset");
                sqlParams.Add(new NpgsqlParameter("code",pageRequest.code));
                sqlParams.Add(new NpgsqlParameter("limit", pageRequest.Limit));
                sqlParams.Add(new NpgsqlParameter("offset", pageRequest.Limit * pageRequest.PageIndex));
                sqlParams.Add(new NpgsqlParameter("startime", Convert.ToDateTime(pageRequest.date[0])));
                sqlParams.Add(new NpgsqlParameter("endtime", (formattedDate)));
                var datas = _dbContext.ZmPcYhLogCjDto.FromSqlRaw(sbSql.ToString(), sqlParams.ToArray()).ToList();
                var pageData = new Page<ZmPcYhLogCjDto>(datas
                    , pageRequest.PageIndex + 1, pageRequest.Limit, count);
                var r = new RespRet<Page<ZmPcYhLogCjDto>>(true, "Ok", pageData);
                return r;
            }
            catch (Exception ex)
            {
                var res = new RespRet<Page<ZmPcYhLogCjDto>>(false, ex.ToString());
                WriteSystemLogError("housescj/search-wzcfwdetail", res, pageRequest);
                res.Msg = "系统异常";
                return res;
            }
        }





        /// <summary>
        /// 窗口护栏拆除zhuzhuangtu
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/ckhlccbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> ckhlccbar([FromBody] DateQuery param)
        {

            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count() != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            var isxz = true;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                isxz = false;
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" + DateTime.Now.Date.Year;
            string sql = @" ";
            #region
            if (param.code.Length == 3 || param.code.Length == 6)
            {
                sql = $@" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00 when ckfhwggptzlsl=0 then 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (	select  zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select sum(zcount)zcount,xzqdm code,max(xzq) name, case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  (SELECT t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (( SELECT count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""其它""
           FROM zm_buildings
          WHERE (NOT ((zm_buildings.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_buildings.xzqdm, zm_buildings.xzq) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_on,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.xzqdm, zm_pc_base.xzq) t2 ON (((t1.xzqdm)::text = (t2.xzqdm)::text)))
  ORDER BY t1.xzqdm)jbxxgqview GROUP BY xzqdm) a left join (select xzqdm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  (SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text =@endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
) a   group by xzqdm)  b	 on a.code=b.xzqdm)a ORDER BY ccl desc,code  
       ";
            }
            if (param.code.Length == 6 && isxz)
            {
                sql = $@" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl  ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ckfhwggptzlsl=0 then 100.00 when ccl is null then 0.00 ELSE ccl END  ccl from (				
		select   zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,towndm code,max(town) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  (SELECT t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM ((( SELECT count(1) AS zcount,
            zm_buildings.towndm,
            zm_buildings.xzq,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""其它"",
            zm_buildings.xzqdm
           FROM zm_buildings
          WHERE (NOT ((zm_buildings.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_buildings.towndm, zm_buildings.xzq, zm_buildings.xzqdm
          ORDER BY zm_buildings.towndm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.town
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs,
                    t2_1.fwzt
                   FROM (zm_pc_base t1_1
                     LEFT JOIN zm_pc_base_cj t2_1 ON (((t1_1.fwbh)::text = (t2_1.fwbh)::text)))) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.town) t2 ON (((t1.towndm)::text = (t2.town)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
  ORDER BY t1.towndm)jbxxxzview where towndm like @code group by towndm) a left join  (select towndm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  (SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
) a   group by towndm )  b on a.code=b.towndm )a ORDER BY ccl desc,code

	";
            }

            if (param.code.Length == 9)
            {
                sql = $@" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00 when ckfhwggptzlsl=0 then 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
		select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  (SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code  ";
            }
            if (param.code.Length == 12)
            {
                sql = $@" select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00 when ckfhwggptzlsl=0 then 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
		select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  (SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code  ";    
            }
            #endregion
            var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime =formattedDate}).ToList();
            return new RespRet<object>(true, "Ok", data);
        }













        /// <summary>
        /// 窗口护栏拆除
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/ckhlccmsg")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> ckhlccmsg([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" + DateTime.Now.Date.Year;
            string sql = @" ";
            if (param.code.Length == 3 || param.code.Length == 6)
            {
                sql = $@" select sum(zcount) ckfhwggptzlsl,sum(ccfhwggptzlsl)ccfhwggptzlsl,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/case when sum(zcount)=0 then 1 else sum(zcount) end,0.00),2)  ccl from (  select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (	select  zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select sum(zcount)zcount,xzqdm code,max(xzq) name, case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  ( SELECT t1.xzq,
    t1.xzqdm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (( SELECT count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.xzqdm)::text = (zm_buildings.xzqdm)::text)) AS ""其它""
           FROM zm_buildings
          WHERE (NOT ((zm_buildings.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_buildings.xzqdm, zm_buildings.xzq) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_on,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.xzqdm, zm_pc_base.xzq) t2 ON (((t1.xzqdm)::text = (t2.xzqdm)::text)))
  ORDER BY t1.xzqdm)jbxxgqview GROUP BY xzqdm) a left join (select xzqdm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  ( SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text =@endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
) a   group by xzqdm)  b	 on a.code=b.xzqdm)a ORDER BY ccl desc,code) a	 ";
            }
            if (param.code.Length == 9)
            {
                sql = $@" 				select sum(zcount) ckfhwggptzlsl,sum(ccfhwggptzlsl)ccfhwggptzlsl,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/case when sum(zcount)=0 then 1 else sum(zcount) end,0.00),2)  ccl from( select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl  ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
		select   zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,towndm code,max(town) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  ( SELECT t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM ((( SELECT count(1) AS zcount,
            zm_buildings.towndm,
            zm_buildings.xzq,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.town)::text = (zm_buildings.towndm)::text)) AS ""其它"",
            zm_buildings.xzqdm
           FROM zm_buildings
          WHERE (NOT ((zm_buildings.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_buildings.towndm, zm_buildings.xzq, zm_buildings.xzqdm
          ORDER BY zm_buildings.towndm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.town
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs,
                    t2_1.fwzt
                   FROM (zm_pc_base t1_1
                     LEFT JOIN zm_pc_base_cj t2_1 ON (((t1_1.fwbh)::text = (t2_1.fwbh)::text)))) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.town) t2 ON (((t1.towndm)::text = (t2.town)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
  ORDER BY t1.towndm)jbxxxzview where towndm like @code group by towndm) a left join  (select towndm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
		select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  ( SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
) a   group by towndm )  b on a.code=b.towndm )a ORDER BY ccl desc,code)a
				  ";
            }
            if (param.code.Length == 12)
            {
                sql = $@" select sum(zcount) ckfhwggptzlsl,sum(ccfhwggptzlsl)ccfhwggptzlsl,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/case when sum(zcount)=0 then 1 else sum(zcount) end,0.00),2)  ccl from (select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
		select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  ( SELECT t4.region_name AS village,
    t1.villagedm,
    t1.xzq,
    t1.xzqdm,
    t3.region_name AS town,
    t1.towndm,
    COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
    COALESCE(t2.jzrks, (0)::bigint) AS jzrks,
    COALESCE(t2.czryrs, (0)::bigint) AS czryrs,
    COALESCE(t2.cjzwqtjyyt, (0)::bigint) AS cjzwqtjyyt,
    COALESCE(t2.zzrs, (0)::bigint) AS zzrs,
    COALESCE(t2.fjsl, (0)::bigint) AS fjsl,
    COALESCE(t2.jzfjs, (0)::bigint) AS jzfjs,
    COALESCE(t2.jzscxyaqcksl, (0)::bigint) AS jzscxyaqcksl,
    COALESCE(t2.jzxyssltsl, (0)::bigint) AS jzxyssltsl,
    COALESCE(t2.ckfhwggptzlsl, (0)::bigint) AS ckfhwggptzlsl,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl,
    COALESCE(t2.mhqsl, (0)::bigint) AS mhqsl,
    COALESCE(t2.fytsmz, (0)::bigint) AS fytsmz,
    COALESCE(t2.xggdjsbp, (0)::bigint) AS xggdjsbp,
    COALESCE(t2.jzsyjyplmhxts, (0)::bigint) AS jzsyjyplmhxts,
    COALESCE(t2.jzszlwxdlgybjqs, (0)::bigint) AS jzszlwxdlgybjqs,
    COALESCE(t2.wdwhjgfjgqsyyrkrgbdj, (0)::bigint) AS wdwhjgfjgqsyyrkrgbdj,
    COALESCE(t2.sstdsyyrkrclzx, (0)::bigint) AS sstdsyyrkrclzx,
    COALESCE(t2.gryssdcsszblyyqksddp, (0)::bigint) AS gryssdcsszblyyqksddp,
    COALESCE(t2.csjyssl, (0)::bigint) AS csjyssl,
    COALESCE(t2.lcjzsl, (0)::bigint) AS lcjzsl,
    COALESCE(t2.ssltdbfjsl, (0)::bigint) AS ssltdbfjsl,
    COALESCE(t2.sstdywc, (0)::bigint) AS sstdywc,
    COALESCE(t2.zzsggz, (0)::bigint) AS zzsggz,
    COALESCE(t2.zzzdfa, (0)::bigint) AS zzzdfa,
    COALESCE(t2.jzswhntjzssslt, (0)::bigint) AS jzswhntjzssslt,
    COALESCE(t2.jzsnzssslt, (0)::bigint) AS jzsnzssslt,
    COALESCE((t1.""餐饮"")::numeric, (0)::numeric) AS ""餐饮"",
    COALESCE((t1.""商店"")::numeric, (0)::numeric) AS ""商店"",
    COALESCE((t1.""美容美发"")::numeric, (0)::numeric) AS ""美容美发"",
    COALESCE((t1.""KTV"")::numeric, (0)::numeric) AS ""KTV"",
    COALESCE((t1.""网吧"")::numeric, (0)::numeric) AS ""网吧"",
    COALESCE((t1.""美甲美睫"")::numeric, (0)::numeric) AS ""美甲美睫"",
    COALESCE((t1.""棋牌室"")::numeric, (0)::numeric) AS ""棋牌室"",
    COALESCE((t1.""茶社"")::numeric, (0)::numeric) AS ""茶社"",
    COALESCE((t1.""足疗、按摩"")::numeric, (0)::numeric) AS ""足疗、按摩"",
    COALESCE((t1.""教学培训"")::numeric, (0)::numeric) AS ""教学培训"",
    COALESCE((t1.""医疗诊所"")::numeric, (0)::numeric) AS ""医疗诊所"",
    COALESCE((t1.""加工生产"")::numeric, (0)::numeric) AS ""加工生产"",
    COALESCE((t1.""其它"")::numeric, (0)::numeric) AS ""其它"",
    COALESCE(t2.lyxlhxjjzss, (0)::bigint) AS lyxlhxjjzss
   FROM (((( SELECT count(1) AS zcount,
            mm.villagedm,
            mm.xzq,
            mm.xzqdm,
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%餐饮%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""餐饮"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%商店%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""商店"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美容美发%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美容美发"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%KTV%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""KTV"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%网吧%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""网吧"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%美甲美睫%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""美甲美睫"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%棋牌室%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""棋牌室"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%茶社%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""茶社"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%足疗、按摩%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""足疗、按摩"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%教学培训%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""教学培训"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%医疗诊所%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""医疗诊所"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%加工生产%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""加工生产"",
            ( SELECT sum(
                        CASE
                            WHEN ((zm_pc_base.cjzwqtjyytmsg)::text ~~ '%其它%'::text) THEN 1
                            ELSE 0
                        END) AS sum
                   FROM zm_pc_base
                  WHERE ((zm_pc_base.village)::text = (mm.villagedm)::text)) AS ""其它"",
            mm.towndm
           FROM zm_buildings mm
          WHERE (NOT ((mm.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY mm.villagedm, mm.xzq, mm.xzqdm, mm.towndm
          ORDER BY mm.villagedm) t1
     LEFT JOIN ( SELECT sum(zm_pc_base.jzrks) AS jzrks,
            sum(zm_pc_base.zzrs) AS zzrs,
            sum(zm_pc_base.czryrs) AS czryrs,
            sum(zm_pc_base.cjzwqtjyyt) AS cjzwqtjyyt,
            sum(zm_pc_base.fjsl) AS fjsl,
            sum(zm_pc_base.jzfjs) AS jzfjs,
            sum(zm_pc_base.jzscxyaqcksl) AS jzscxyaqcksl,
            sum(zm_pc_base.jzxyssltsl) AS jzxyssltsl,
            sum(zm_pc_base.ckfhwggptzlsl) AS ckfhwggptzlsl,
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            sum(zm_pc_base.mhqsl) AS mhqsl,
            sum(zm_pc_base.fytsmz) AS fytsmz,
            sum(zm_pc_base.xggdjsbp) AS xggdjsbp,
            sum(zm_pc_base.jzsyjyplmhxts) AS jzsyjyplmhxts,
            sum(zm_pc_base.jzszlwxdlgybjqs) AS jzszlwxdlgybjqs,
            sum(zm_pc_base.wdwhjgfjgqsyyrkrgbdj) AS wdwhjgfjgqsyyrkrgbdj,
            sum(zm_pc_base.sstdsyyrkrclzx) AS sstdsyyrkrclzx,
            sum(zm_pc_base.gryssdcsszblyyqksddp) AS gryssdcsszblyyqksddp,
            sum(
                CASE
                    WHEN (zm_pc_base.cs >= 3) THEN 1
                    ELSE 0
                END) AS csjyssl,
            sum(
                CASE
                    WHEN (zm_pc_base.cs = 2) THEN 1
                    ELSE 0
                END) AS lcjzsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.cs = 1) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    WHEN ((zm_pc_base.cs >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2) AND (zm_pc_base.jzscxyaqcksl >= 2)) THEN 1
                    ELSE 0
                END) AS ssltdbfjsl,
            sum(
                CASE
                    WHEN ((zm_pc_base.jsrysstd)::text = '已完成'::text) THEN 1
                    ELSE 0
                END) AS sstdywc,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在施工改造'::text) THEN 1
                    ELSE 0
                END) AS zzsggz,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text = '正在制定方案'::text) THEN 1
                    ELSE 0
                END) AS zzzdfa,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室外或内天井增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzswhntjzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%建筑室内增设疏散楼梯%'::text) THEN 1
                    ELSE 0
                END) AS jzsnzssslt,
            sum(
                CASE
                    WHEN ((zm_pc_base.sstdjsqk)::text ~~ '%利用相邻或相近建筑疏散%'::text) THEN 1
                    ELSE 0
                END) AS lyxlhxjjzss,
            zm_pc_base.village,
            zm_pc_base.town,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM ( SELECT t1_1.id,
                    t1_1.fwbh,
                    t1_1.yhly,
                    t1_1.xzqdm,
                    t1_1.town,
                    t1_1.village,
                    t1_1.address,
                    t1_1.yhfwbh,
                    t1_1.hzxm,
                    t1_1.jzrks,
                    t1_1.zzrs,
                    t1_1.cs,
                    t1_1.fjsl,
                    t1_1.jzfjs,
                    t1_1.jzscxyaqcksl,
                    t1_1.jzxyssltsl,
                    t1_1.jzscmj,
                    t1_1.ckfhwggptzlsl,
                    t1_1.bdfwjzrydddzxcsl,
                    t1_1.mhqsl,
                    t1_1.fytsmz,
                    t1_1.xggdjsbp,
                    t1_1.jzsyjyplmhxts,
                    t1_1.jzszlwxdlgybjqs,
                    t1_1.wdwhjgfjgqsyyrkrgbdj,
                    t1_1.sstdsyyrkrclzx,
                    t1_1.gryssdcsszblyyqksddp,
                    t1_1.cjzwqtjyytmsg,
                    t1_1.jsrysstd,
                    t1_1.sstdjsqk,
                    t1_1.create_by,
                    t1_1.create_on,
                    t1_1.create_ip,
                    t1_1.modified_by,
                    t1_1.modified_on,
                    t1_1.modified_ip,
                    t1_1.is_delete,
                    t1_1.delete_by,
                    t1_1.delete_on,
                    t1_1.delete_ip,
                    t1_1.pcsj,
                    t1_1.cjzwqtjyyt,
                    t1_1.xzq,
                    t1_1.towndm,
                    t1_1.villagedm,
                    t1_1.cdfs,
                    t1_1.cdfsqtbz,
                    t1_1.czryrs
                   FROM zm_pc_base t1_1) zm_pc_base
          WHERE (NOT ((zm_pc_base.fwbh)::text IN ( SELECT {table_name}.fwbh
                   FROM {table_name}
                  WHERE (({table_name}.final_fwzt IS NOT NULL) AND (({table_name}.final_create_on)::text = @endtime::text)))))
          GROUP BY zm_pc_base.village, zm_pc_base.town, zm_pc_base.xzq, zm_pc_base.xzqdm) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
     LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text)))
     LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text)))
  ORDER BY t1.villagedm)jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime::TIMESTAMP group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code )a	";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate }).FirstOrDefault();
            return new RespRet<object>(true, "Ok", msg);
        }









        /// <summary>
        ///以防护栏G个数为单位统计
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/fhlgspie")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> fhlgspie([FromBody] DateQuery param)
        {

            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

            string sql = @" ";
            if (param.code.Length == 3 || param.code.Length == 6)
            {
                sql = @" 
select '已拆除' name ,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/case when sum(zcount)=0 then 1 else sum(zcount) end,0.00),2)  value from (  select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (	select  zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select sum(zcount)zcount,xzqdm code,max(xzq) name, case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxgqview GROUP BY xzqdm) a left join (select xzqdm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
					select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
   ) a   group by xzqdm)  b	 on a.code=b.xzqdm)a ORDER BY ccl desc,code) a	 ";
            }
            if (param.code.Length == 9)
            {
                sql = @" select '已拆除' name ,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/case when sum(zcount)=0 then 1 else sum(zcount) end,0.00),2)  value from( select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl  ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
					select   zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,towndm code,max(town) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxxzview where towndm like @code group by towndm) a left join  (select towndm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
					select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
   ) a   group by towndm )  b on a.code=b.towndm )a ORDER BY ccl desc,code)a
	 ";
            }
            if (param.code.Length == 12)
            {
                sql = @" select '已拆除' name ,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/case when sum(zcount)=0 then 1 else sum(zcount) end,0.00),2)  value from (select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
					select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code )a
           ";
            }
            var tb = _dbContext.Database.GetDbConnection().Query<PieDto>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
            tb.Add(new PieDto { name = "未拆除", value = 100 - tb[0].value });
            return new RespRet<object>(true, "Ok", tb);
        }




















//        /// <summary>
//        ///以防护栏G个数为单位统计
//        /// </summary>
//        /// <param name="pageRequest"></param>
//        /// <returns></returns>
//        [HttpPost("housescj/fhlgspie")]
//        [Produces("application/json")]
//        [ProducesResponseType(typeof(RespRet<object>), 200)]
//        public RespRet<object> fhlgspie([FromBody] DateQuery param)
//        {

//            var usernNumber = this.CurrentUser();
//            if (param.date == null || param.date.Count != 2)
//            {
//                return new RespRet<object>(false, "时间不能为空");
//            }
//            param.date[1] = param.date[1].AddHours(23);
//            param.date[1] = param.date[1].AddMinutes(59);
//            param.date[1] = param.date[1].AddSeconds(59);
//            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
//            var a = JsonRegion(user.District);
//            JObject obj = JObject.Parse(user.District);
//            string RegionCode = (string)obj["RegionCode"];
//            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
//            if (string.IsNullOrWhiteSpace(param.code))
//            {
//                param.code = RegionCode;
//            }
//            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

//            string sql = @" ";
//            if (param.code.Length == 3 || param.code.Length == 6)
//            {
//                sql = @" 
//select '已拆除' name ,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/sum(zcount),0.00),2)  value from (  select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (	select  zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
//			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select sum(zcount)zcount,xzqdm code,max(xzq) name, case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxgqview GROUP BY xzqdm) a left join (select xzqdm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
//					select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
//			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
//   ) a   group by xzqdm)  b	 on a.code=b.xzqdm)a ORDER BY ccl desc,code) a	 ";
//            }
//            if (param.code.Length == 9)
//            {
//                sql = @" select '已拆除' name ,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/sum(zcount),0.00),2)  value from( select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl  ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
//					select   zcount,code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
//			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,towndm code,max(town) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxxzview where towndm like @code group by towndm) a left join  (select towndm,sum(ccfhwggptzlsl) ccfhwggptzlsl From 	(select xzqdm,xzq,towndm,town,ckfhwggptzlsl zcount,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code  from (				
//					select xzqdm,xzq,towndm,town,zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
//			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,max(xzqdm)xzqdm,max(xzq)xzq,max(towndm)towndm,max(town)town,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview  group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where  create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code
//   ) a   group by towndm )  b on a.code=b.towndm )a ORDER BY ccl desc,code)a
//	 ";
//            }
//            if (param.code.Length == 12)
//            {
//                sql = @" select '已拆除' name ,round(COALESCE(sum(ccfhwggptzlsl)*1.00*100/sum(zcount),0.00),2)  value from (select ckfhwggptzlsl zcount,(COALESCE(ckfhwggptzlsl,0) -COALESCE(ccfhwggptzlsl,0)) sy,case when COALESCE(ccfhwggptzlsl,0)>COALESCE(ckfhwggptzlsl,0) then COALESCE(ckfhwggptzlsl,0) else COALESCE(ccfhwggptzlsl,0) end  ccfhwggptzlsl ,name , code,CASE WHEN ccl > 100 THEN 100.00  when ccl is null then 0.00 ELSE ccl END  ccl from (				
//					select  zcount, code,name,case when ckfhwggptzlsl is null then 0 else ckfhwggptzlsl end ckfhwggptzlsl,case when ccfhwggptzlsl is null then 0 else ccfhwggptzlsl end ccfhwggptzlsl  ,
//			case when 	round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) is null then 0.00 else round(ccfhwggptzlsl*1.0*100/ckfhwggptzlsl,2) end  ccl  from (select  sum(zcount)zcount,villagedm code,max(village) name,case when sum(ckfhwggptzlsl)=0 then null else  sum(ckfhwggptzlsl) end ckfhwggptzlsl from  jbxxcjview where villagedm like @code group by villagedm) a left join  (select villagedm,sum(ccfhwggptzlsl) ccfhwggptzlsl from zm_pc_gjzz_cjlog where villagedm like @code and create_on between  @startime and @endtime group by villagedm)  b on a.code=b.villagedm )a ORDER BY ccl desc,code )a
//           ";
//            }
//            var tb = _dbContext.Database.GetDbConnection().Query<PieDto>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
//            tb.Add(new PieDto { name = "未拆除", value = 100 - tb[0].value });
//            return new RespRet<object>(true, "Ok", tb);
//        }

















        /// <summary>
        ///以防护栏G个数为单位统计
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpGet("housescj/fhlfzdspie")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Fhlfzdspie([FromQuery] string code)
        {

            var usernNumber = this.CurrentUser();
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(code))
            {
                code = RegionCode;
            }
            code = code.Substring(code.Length - 3) == "000" ? code.Substring(0, code.Length - 3) : code;

            string sql = @" ";
            if (code.Length == 3)
            {
                sql = @" select  count(case when  ckfhwggptzlsl>0  then 1 end)-(select count( case when ccfhwggptzlsl>0 then 1 end )value  from zm_pc_gjzz_cj) value,'剩余'name from  zm_pc_base
		union all 
		select count( case when ccfhwggptzlsl>0 then 1 end )value ,'已拆除' name from zm_pc_gjzz_cj ";
            }
            if (code.Length == 6)
            {
                sql = @" 	select  count(case when  ckfhwggptzlsl>0  then 1 end)-(select count( case when ccfhwggptzlsl>0 then 1 end )value  from zm_pc_gjzz_cj where xzqdm like @code ) value,'剩余'name from  zm_pc_base where xzqdm like @code
		union all 
		select count( case when ccfhwggptzlsl>0 then 1 end )value ,'已拆除' name from zm_pc_gjzz_cj where xzqdm like @code  ";
            }
            if (code.Length == 9)
            {
                sql = @" 	select  count(case when  ckfhwggptzlsl>0  then 1 end)-(select count( case when ccfhwggptzlsl>0 then 1 end )value  from zm_pc_gjzz_cj where towndm like @code ) value,'剩余'name from  zm_pc_base where xzqdm like @code
		union all 
		select count( case when ccfhwggptzlsl>0 then 1 end )value ,'已拆除' name from zm_pc_gjzz_cj where towndm like @code  ";
            }
            if (code.Length == 12)
            {
                sql = @" 		select  count(case when  ckfhwggptzlsl>0  then 1 end)-(select count( case when ccfhwggptzlsl>0 then 1 end )value  from zm_pc_gjzz_cj where villagedm like @code ) value,'剩余'name from  zm_pc_base where xzqdm like @code
		union all 
		select count( case when ccfhwggptzlsl>0 then 1 end )value ,'已拆除' name from zm_pc_gjzz_cj where villagedm like @code ";

            }
            var tb = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + code + "%" }).ToList();
            return new RespRet<object>(true, "Ok", tb);
        }



        /// <summary>
        /// 电动自行车违规充电
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/ddzxcwgcdmsg")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> ddzxcwgcdmsg([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" +DateTime.Now.Date.Year;
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            string sql = @$" select  (select sum(bdfwjzrydddzxcsl) bdfwjzrydddzxcsl from (   SELECT t1.xzqdm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm
           FROM  zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
          ORDER BY zm_buildings.xzqdm) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.xzqdm, zm_pc_base.xzq) t2 ON (((t1.xzqdm)::text = (t2.xzqdm)::text))) left join (select t1.xzqdm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in ( SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime) GROUP BY t1.xzqdm) t3 on t2.xzqdm=t3.xzqdm
  ORDER BY t1.xzqdm)a),COALESCE(sum(fx),0)zs ,COALESCE(sum(zg),0)qlddcsl, case when  round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2) is null then 0.00 when round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2)>100 then 100.00 else  round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.xzqdm from  zcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.xzqdm=b.xzqdm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.xzqdm from  (select sum(zg_ddc_num) zc,b.xzqdm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where a.fwbh not in (SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime) and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.xzqdm) a full join (select sum(zg_ddc_num) jc,b.xzqdm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.xzqdm)b on a.xzqdm=b.xzqdm) t2 on t1.xzqdm=t2.xzqdm  ";
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            var lea = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Length - 3 : param.code.Length;
            //            if (lea == 6)
            //            {
            //                sql = @$" select  sum(fx)zs ,sum(zg)qlddcsl, case when  round(sum(zg)*100*1.00/sum(fx),2) is null then 0.00 when round(sum(zg)*100*1.00/sum(fx),2)>100 then 100.00 else  round(sum(zg)*100*1.00/sum(fx),2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.xzqdm from  zcyhgqview a,jcyhgqview b where a.xzqdm=b.xzqdm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.xzqdm from  (select sum(zg_ddc_num) zc,b.xzqdm from zm_pc_yhlog_dxzgson a,zm_building_ledger b where a.villagedm=b.villagedm GROUP BY b.xzqdm) a full join (select sum(zg_ddc_num) jc,b.xzqdm from zm_pc_yhlog_dxzgson_cj a,zm_building_ledger b where a.villagedm=b.villagedm GROUP BY b.xzqdm)b on a.xzqdm=b.xzqdm) t2 on t1.xzqdm=t2.xzqdm  
            //where t1.xzqdm like @code ";
            //            }
            if (lea == 9)
            {
                sql = @$"  select  (select sum(bdfwjzrydddzxcsl) bdfwjzrydddzxcsl from ( SELECT t1.towndm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.town),
            zm_buildings.towndm
           FROM zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.towndm
          ORDER BY zm_buildings.towndm) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.town
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.town) t2 ON (((t1.towndm)::text = (t2.town)::text))) left join (select t1.towndm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )  GROUP BY t1.towndm) t3 on t1.towndm=t3.towndm)a where towndm like @code),COALESCE(sum(fx),0)zs ,COALESCE(sum(zg),0)qlddcsl, case when  round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2) is null then 0.00 when round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2)>100 then 100.00 else  round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,COALESCE(a.towndm,b.towndm) towndm from  zcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) b where a.towndm=b.towndm)t1 full join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,COALESCE(a.towndm,b.towndm) towndm from  (select sum(zg_ddc_num) zc, b.towndm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh  where  a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm) a full join (select sum(zg_ddc_num) jc,b.towndm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm)b on a.towndm=b.towndm) t2 on t1.towndm=t2.towndm  where t1.towndm like @code 
              ";
            }
            if (lea == 12)
            {
                sql = @$" select  (select sum(bdfwjzrydddzxcsl) bdfwjzrydddzxcsl from ( SELECT t1.villagedm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.village),
            zm_buildings.villagedm
           FROM zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.villagedm
          ) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.village
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text))) left join (select t1.villagedm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) GROUP BY t1.villagedm) t3 on t1.villagedm=t3.villagedm)a where villagedm like @code),COALESCE(sum(fx),0)zs ,COALESCE(sum(zg),0)qlddcsl, case when  round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2) is null then 0.00 when round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2)>100 then 100.00 else  round(sum(zg)*100*1.00/case when 	sum(fx)=0 then null else sum(fx) end,2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.villagedm from  zcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.villagedm=b.villagedm)t1 full join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,COALESCE(a.villagedm,b.villagedm) villagedm from  (select sum(zg_ddc_num) zc,b.villagedm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh  where a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm) a full join (select sum(zg_ddc_num) jc,b.villagedm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm)b on a.villagedm=b.villagedm) t2 on t1.villagedm=t2.villagedm  where t1.villagedm like @code 
	  ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate }).FirstOrDefault();
            return new RespRet<object>(true, "Ok", msg);
        }

        /// <summary>
        /// 清理室内电动自行车情况
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/ddzxcwgcdbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Ddzxcwgcdbar([FromBody] DateQuery param)
        {
            try
            {
                var usernNumber = this.CurrentUser();
                if (param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "时间不能为空");
                }

                if (param.date[1] > DateTime.Now)
                {
                    param.date[1] = DateTime.Today;
                }
                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddMinutes(59);
                param.date[1] = param.date[1].AddSeconds(59);
                  string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" +DateTime.Now.Date.Year;
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                var isxz = true;
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                string sql = @$" select bdfwjzrydddzxcsl, t3.xzqdm code,t3.xzq name,COALESCE(fx,0) zs ,COALESCE(zg,0)qlddcsl, case when  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) is null then 0.00 when round(zg*100*1.00/case when 	fx=0 then null else fx end,2)>100 then 100.00 else  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.xzqdm from  zcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.xzqdm=b.xzqdm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.xzqdm from  (select sum(zg_ddc_num) zc,b.xzqdm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh  where a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and  a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.xzqdm) a full join (select sum(zg_ddc_num) jc,b.xzqdm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.xzqdm)b on a.xzqdm=b.xzqdm) t2 on t1.xzqdm=t2.xzqdm  RIGHT JOIN  (SELECT t1.xzqdm,t1.xzq,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm
           FROM zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
          ORDER BY zm_buildings.xzqdm) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.xzqdm, zm_pc_base.xzq) t2 ON (((t1.xzqdm)::text = (t2.xzqdm)::text))) left join (select t1.xzqdm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) GROUP BY t1.xzqdm) t3 on t2.xzqdm=t3.xzqdm
  ORDER BY t1.xzqdm) t3 on t3.xzqdm=t2.xzqdm ORDER BY t3.xzqdm ";
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
                var lea = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Length - 3 : param.code.Length;
                if (lea == 6 && isxz)
                {
                    sql = @$" select t3.xzqdm,t3.xzq,bdfwjzrydddzxcsl, t3.towndm code,t3.town name,COALESCE(fx,0) zs ,COALESCE(zg,0)qlddcsl, case when  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) is null then 0.00 when round(zg*100*1.00/case when 	fx=0 then null else fx end,2)>100 then 100.00 else  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,COALESCE(a.towndm,b.towndm) towndm from  zcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) b where a.towndm=b.towndm)t1 full join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,COALESCE(a.towndm,b.towndm) towndm from  (select sum(zg_ddc_num) zc, b.towndm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh  where  a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm) a full join (select sum(zg_ddc_num) jc,b.towndm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm)b on a.towndm=b.towndm) t2 on t1.towndm=t2.towndm
right join (	select bdfwjzrydddzxcsl,xzq,xzqdm, towndm,town from ( SELECT t1.towndm,t1.town,xzq,xzqdm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
	  max(zm_buildings.xzq) xzq,
		 max(zm_buildings.xzqdm) xzqdm,
           max(zm_buildings.town) town,
            zm_buildings.towndm
           FROM zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.towndm
          ORDER BY zm_buildings.towndm) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.town
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.town) t2 ON (((t1.towndm)::text = (t2.town)::text))) left join (select t1.towndm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) GROUP BY t1.towndm) t3 on t1.towndm=t3.towndm)a ) t3 on t1.towndm=t3.towndm
where t3.towndm like @code ";}
                if (lea == 9)
                {
                    sql = @$" select  towndm,town,xzqdm,xzq,bdfwjzrydddzxcsl, t3.villagedm code,t3.village name,COALESCE(fx,0) zs ,COALESCE(zg,0)qlddcsl, case when  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) is null then 0.00 when round(zg*100*1.00/case when 	fx=0 then null else fx end,2)>100 then 100.00 else  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) end wcl  from (select COALESCE(fx,0) fx,COALESCE(zg,0)zg,COALESCE(t1.villagedm,t2.villagedm) villagedm from (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.villagedm from  zcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.villagedm=b.villagedm)t1 full join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,COALESCE(a.villagedm,b.villagedm) villagedm from  (select sum(zg_ddc_num) zc,b.villagedm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh  where  a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm) a full join (select sum(zg_ddc_num) jc,b.villagedm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm)b on a.villagedm=b.villagedm) t2 on t1.villagedm=t2.villagedm)t2 
right join 
(SELECT t1.villagedm,t1.village,t1.towndm,t1.town,t1.xzq,t1.xzqdm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.village)village,
					 max(zm_buildings.towndm) towndm,
					 max(zm_buildings.town)town,
					 max(zm_buildings.xzqdm)xzqdm,
					 max(zm_buildings.xzq)xzq,
            zm_buildings.villagedm
           FROM zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.villagedm
          ) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.village
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text))) left join (select t1.villagedm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )  GROUP BY t1.villagedm) t3 on t1.villagedm=t3.villagedm) t3 on t3.villagedm=t2.villagedm
where t3.villagedm like @code ";  }
                if (lea == 12)
                {
                    sql = @$" select  towndm,town,xzqdm,xzq,bdfwjzrydddzxcsl, t3.villagedm code,t3.village name,COALESCE(fx,0) zs ,COALESCE(zg,0)qlddcsl, case when  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) is null then 0.00 when round(zg*100*1.00/case when 	fx=0 then null else fx end,2)>100 then 100.00 else  round(zg*100*1.00/case when 	fx=0 then null else fx end,2) end wcl  from (select COALESCE(fx,0) fx,COALESCE(zg,0)zg,COALESCE(t1.villagedm,t2.villagedm) villagedm from (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.villagedm from  zcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.villagedm=b.villagedm)t1 full join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,COALESCE(a.villagedm,b.villagedm) villagedm from  (select sum(zg_ddc_num) zc,b.villagedm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh  where  a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm) a full join (select sum(zg_ddc_num) jc,b.villagedm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and a.create_on between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm)b on a.villagedm=b.villagedm) t2 on t1.villagedm=t2.villagedm)t2 
right join 
(SELECT t1.villagedm,t1.village,t1.towndm,t1.town,t1.xzq,t1.xzqdm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.village)village,
					 max(zm_buildings.towndm) towndm,
					 max(zm_buildings.town)town,
					 max(zm_buildings.xzqdm)xzqdm,
					 max(zm_buildings.xzq)xzq,
            zm_buildings.villagedm
           FROM zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_buildings.villagedm
          ) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.village
           FROM (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
                   FROM zm_buildings))   and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )
          GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text))) left join (select t1.villagedm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1  where t1.create_on between @startime and @endtime::TIMESTAMP and fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )  GROUP BY t1.villagedm) t3 on t1.villagedm=t3.villagedm) t3 on t3.villagedm=t2.villagedm
where t3.villagedm like @code "; }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime =formattedDate }).ToList();
                Dictionary<string, object> dictionary = new Dictionary<string, object>();

                var res = new RespRet<object>(true, "Ok", data);
               // WriteSystemLog("housescj/ddzxcwgcdbar", res, param.code);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString(), param.code);
                WriteSystemLogError("houses/qlsnddzxcqk", res, param.code);
                res.Msg = "系统异常";
                return res;
            }
        }




        /// <summary>
        /// 违规使用液化石油气
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/wgsyyhsyqmsg")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> Wgsyyhsyqmsg([FromBody] DateQuery param)
        {

            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = RegionCode;
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" + param.date[1].Year;
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            //if (!code.Contains(RegionCode))
            //{
            //    var error = new RespRet<object>(false, "未在管辖范围内");
            //    WriteSystemLog("housescj/wgsyyhsyqmsg", error, null);
            //    return error;
            //}
            string sql = @$" select COALESCE(sum(fx),0)fx ,COALESCE(sum(zg),0)zg, COALESCE(case when  round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2) is null then 0.00 when round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2)>100 then 100.00 else  round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2) end,0.00) wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.xzqdm from  zcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.xzqdm=b.xzqdm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.xzqdm from  (select sum(zg_yhsyq_num) zc,b.xzqdm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.xzqdm) a full join (select sum(zg_yhsyq_num) jc,b.xzqdm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.xzqdm)b on a.xzqdm=b.xzqdm) t2 on t1.xzqdm=t2.xzqdm   ";
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            var lea = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Length - 3 : param.code.Length;
            //            if (lea == 6)
            //            {
            //                sql = @$" select  sum(fx)zs ,sum(zg)qlddcsl, case when  round(sum(zg)*100*1.00/sum(fx),2) is null then 0.00 when round(sum(zg)*100*1.00/sum(fx),2)>100 then 100.00 else  round(sum(zg)*100*1.00/sum(fx),2) end wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.xzqdm from  zcyhgqview a,jcyhgqview b where a.xzqdm=b.xzqdm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.xzqdm from  (select sum(zg_yhsyq_num) zc,b.xzqdm from zm_pc_yhlog_dxzgson a,zm_building_ledger b where a.villagedm=b.villagedm GROUP BY b.xzqdm) a full join (select sum(zg_yhsyq_num) jc,b.xzqdm from zm_pc_yhlog_dxzgson_cj a,zm_building_ledger b where a.villagedm=b.villagedm GROUP BY b.xzqdm)b on a.xzqdm=b.xzqdm) t2 on t1.xzqdm=t2.xzqdm  
            //where t1.xzqdm like @code ";
            //            }
            if (lea == 9)
            {
                sql = @$" 		select  COALESCE(sum(fx),0)fx ,COALESCE(sum(zg),0)zg, COALESCE(case when  round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2) is null then 0.00 when round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2)>100 then 100.00 else  round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2) end,0.00) wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.towndm from  zcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) b where a.towndm=b.towndm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.towndm from  (select sum(zg_yhsyq_num) zc,b.towndm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )  and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm) a full join (select sum(zg_yhsyq_num) jc,b.towndm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm)b on a.towndm=b.towndm) t2 on t1.towndm=t2.towndm  where t1.towndm like @code    ";
            }
            if (lea == 12)
            {
                sql = @$"  select  COALESCE(sum(fx),0)fx ,COALESCE(sum(zg),0)zg,COALESCE(case when  round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2) is null then 0.00 when round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2)>100 then 100.00 else  round(sum(zg)*100*1.00/case when sum(fx)=0 then null else sum(fx) end,2) end,0.00) wcl  from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.villagedm from  zcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.villagedm=b.villagedm)t1 join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.villagedm from  (select sum(zg_yhsyq_num) zc,b.villagedm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.villagedm) a full join (select sum(zg_yhsyq_num) jc,b.villagedm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.villagedm)b on a.villagedm=b.villagedm) t2 on t1.villagedm=t2.villagedm  where t1.villagedm like @code  ";
            }

            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime =formattedDate }).FirstOrDefault();
            return new RespRet<object>(true, "Ok", msg);
        }

        /// <summary>
        /// 违规使用液化石油气
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/wgsyyhsyqbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> WgsyyhsyqBar([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            var isxz = true;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                isxz = false;
                param.code = RegionCode;
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" + param.date[1].Year;
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

            if (string.IsNullOrWhiteSpace(param.code))
            {
                param.code = "110000";
            }
            string sql = @$" select  t1.xzqdm AS code,xzq name,fx AS use_bottled_liquefied_gas,COALESCE(zg,0)  AS zxqlyhsyqsl 
from  (select a.xzq xzq, a.use_electric_bicycle+b.use_electric_bicycle fx,a.xzqdm from  zcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhgqtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.xzqdm=b.xzqdm)t1 left join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.xzqdm from  (select sum(zg_yhsyq_num) zc,b.xzqdm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.xzqdm) a full join (select sum(zg_yhsyq_num) jc,b.xzqdm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.xzqdm)b on a.xzqdm=b.xzqdm) t2 on t1.xzqdm=t2.xzqdm  order by t1.xzqdm ";
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            var lea = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Length - 3 : param.code.Length;
            if (lea == 6 && isxz)
            {
                sql = @$"  select t1.towndm AS code,town name,fx AS use_bottled_liquefied_gas,COALESCE(zg,0)  AS zxqlyhsyqsl 
from (select a.town town, a.use_electric_bicycle+b.use_electric_bicycle fx,a.towndm from  zcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhxztext(@startime,@endtime::TIMESTAMP,@tablename) b where a.towndm=b.towndm)t1 left join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.towndm from  (select sum(zg_yhsyq_num) zc,b.towndm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           ) and  date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.towndm) a full join (select sum(zg_yhsyq_num) jc,b.towndm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.towndm)b on a.towndm=b.towndm) t2 on t1.towndm=t2.towndm  where t1.towndm like @code  order by t1.towndm ";

            }
            if (lea == 9)
            {
                sql = @$" select t1.villagedm AS code,village name,fx AS use_bottled_liquefied_gas,COALESCE(zg,0)  AS zxqlyhsyqsl 
from  (select a.village village, a.use_electric_bicycle+b.use_electric_bicycle fx,a.villagedm from  zcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.villagedm=b.villagedm)t1 left join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.villagedm from  (select sum(zg_yhsyq_num) zc,b.villagedm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )  and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm) a full join (select sum(zg_yhsyq_num) jc,b.villagedm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.villagedm)b on a.villagedm=b.villagedm) t2 on t1.villagedm=t2.villagedm  where t1.villagedm like @code order by t1.villagedm  ";
            }
            if (lea == 12)
            {
                sql = @$" select t1.villagedm AS code,village name,fx AS use_bottled_liquefied_gas,COALESCE(zg,0)  AS zxqlyhsyqsl 
from  (select a.village village, a.use_electric_bicycle+b.use_electric_bicycle fx,a.villagedm from  zcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) a,jcyhcjtext(@startime,@endtime::TIMESTAMP,@tablename) b where a.villagedm=b.villagedm)t1 left join (select  (case when zc is null then 0 else zc end+ case when jc is null then 0 else jc end) zg,a.villagedm from  (select sum(zg_yhsyq_num) zc,b.villagedm from zm_pc_yhlog_dxzgson a join zm_buildings b on a.fwbh=b.fwbh where b.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@endtime
           )  and date(a.create_on) between @startime and @endtime::TIMESTAMP  GROUP BY b.villagedm) a full join (select sum(zg_yhsyq_num) jc,b.villagedm from zm_pc_yhlog_dxzgson_cj a join zm_buildings b on a.fwbh=b.fwbh  join zm_pc_yhlog_dx_cj c on a.yhlog_dx_id=c.id join zm_pc_yh_cj d on d.id=c.yhlog_id  where d.jcly='5' and date(a.create_on) between @startime and @endtime::TIMESTAMP GROUP BY b.villagedm)b on a.villagedm=b.villagedm) t2 on t1.villagedm=t2.villagedm  where t1.villagedm like @code order by t1.villagedm  ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new {@tablename=table_name, @code = "%" + param.code + "%", @startime = param.date[0], @endtime = formattedDate}).ToList();

            var res = new RespRet<object>(true, "Ok", msg);
          //  WriteSystemLog("housescj/wgsyyhsyqbar", res, param.code);
            return new RespRet<object>(true, "Ok", msg);
        }


        /// <summary>
        /// 固定警示标牌
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/gdjsbt")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> gdjsbt([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            var isxz = true;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                isxz = false;
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            string sql = @" ";
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string starttime = Convert.ToDateTime(param.date[0] + "").ToString("yyyy-MM-dd");
            string endtime = Convert.ToDateTime(param.date[1] + "").ToString("yyyy-MM-dd");
            string tablename = "";
            string filed = "";
            string fileWhere = "";
            string table_name = "zm_building_fwzt_" + param.date[1].Year;
            if (endtime + "" == today)
            {
                tablename = "zm_pc_base";
            }
            else
            {
                string startimemonth = Convert.ToDateTime(param.date[0] + "").ToString("yyyy_MM");
                string endmonth = Convert.ToDateTime(param.date[1] + "").ToString("yyyy_MM");
                string month = "";
                if (startimemonth == endmonth)
                {
                    month = startimemonth;
                    tablename = "zm_pc_baselog_" + month;
                    filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                    fileWhere = " and row_num=1 ";
                }
                else
                {
                    tablename = "zm_pc_baselog";
                    filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                    fileWhere = " and row_num=1 ";
                }
            }

            if (param.code.Length == 3 || param.code.Length == 6)
            {
                sql = " select * from  gdjsbtbar_gq_total_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','"+ table_name + "') ";
            }

            if (param.code.Length == 6 && isxz)
            {
                sql = " select * from  gdjsbtbar_xz_total_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+ table_name + "') ";
            }

            if (param.code.Length == 9 || param.code.Length == 12)
            {
                sql = " select * from  gdjsbtbar_cj_total_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+ table_name + "') ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<GdjsbtDto>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).FirstOrDefault();
            var tb = new List<object>();
            tb.Add(new { value = msg.azl, name = "已安装" });
            tb.Add(new { value = 100 - msg.azl, name = "未安装" });
            return new RespRet<object>(true, "Ok", new { tb = tb, msg = msg });
        }



        /// <summary>
        /// 安装固定警示标牌柱状图
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/gdjsbtbar")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> gdjsbtbar([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            var isxz = true;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                isxz = false;
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            string sql = @" ";
            string today = DateTime.Now.ToString("yyyy-MM-dd");
            string starttime = Convert.ToDateTime(param.date[0] + "").ToString("yyyy-MM-dd");
            string endtime = Convert.ToDateTime(param.date[1] + "").ToString("yyyy-MM-dd");
            string tablename = "";
            string filed = "";
            string fileWhere = "";
            string table_name = "zm_building_fwzt_" + param.date[1].Year;
            if (endtime + "" == today)
            {
                tablename = "zm_pc_base";
            }
            else
            {
                string startimemonth = Convert.ToDateTime(param.date[0] + "").ToString("yyyy_MM");
                string endmonth = Convert.ToDateTime(param.date[1] + "").ToString("yyyy_MM");
                string month = "";
                if (startimemonth == endmonth)
                {
                    month = startimemonth;
                    tablename = "zm_pc_baselog_" + month;
                    filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                    fileWhere = " and row_num=1 ";
                }
                else
                {
                    tablename = "zm_pc_baselog";
                    filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                    fileWhere = " and row_num=1 ";
                }
            }

            if (param.code.Length == 3 || param.code.Length == 6)
            {
                sql = " select * from  gdjsbtbar_gq_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','"+ table_name + "') ";
            }

            if (param.code.Length == 6 && isxz)
            {
                sql = " select * from  gdjsbtbar_xz_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+table_name+"') ";
            }

            if (param.code.Length == 9 || param.code.Length == 12)
            {
                sql = " select * from  gdjsbtbar_cj_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','" + tablename + "','%" + param.code + "%','"+table_name+"') ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
            var res = new RespRet<object>(true, "Ok", msg);
          //  WriteSystemLog("housescj/wgsyyhsyqbar", res, param.code);
            return new RespRet<object>(true, "Ok", msg);
        }





        //      /// <summary>
        //      /// 安装固定警示标牌柱状图
        //      /// </summary>
        //      /// <param name="pageRequest"></param>
        //      /// <returns></returns>
        //      [HttpPost("housescj/gdjsbtbar")]
        //      [Produces("application/json")]
        //      [ProducesResponseType(typeof(RespRet<object>), 200)]
        //      public RespRet<object> gdjsbtbar([FromBody] DateQuery param)
        //      {
        //          var usernNumber = this.CurrentUser();
        //          if (param.date == null || param.date.Count != 2)
        //          {
        //              return new RespRet<object>(false, "时间不能为空");
        //          }
        //          param.date[1] = param.date[1].AddHours(23);
        //          param.date[1] = param.date[1].AddMinutes(59);
        //          param.date[1] = param.date[1].AddSeconds(59);
        //          var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
        //          var a = JsonRegion(user.District);
        //          JObject obj = JObject.Parse(user.District);
        //          string RegionCode = (string)obj["RegionCode"];
        //          RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
        //          var isxz = true;
        //          if (string.IsNullOrWhiteSpace(param.code))
        //          {
        //              isxz = false;
        //              param.code = RegionCode;
        //          }
        //          param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
        //          string sql = @$" select  xggdjsbpds,zcount,code,xzq AS name,case when value >100 then 100.00 when value is null then 0.00 else value end value  from (  select zcount,xggdjsbpds,   round(xggdjsbpds*1.00*100/zcount,2) value ,xzq,xzqdm code from  ( SELECT 
        // t1.xzq,
        //  t1.xzqdm,
        //  COALESCE(t1.zcount,0) AS zcount,
        //  COALESCE(t2.xggdjsbpds,0) AS xggdjsbpds
        // FROM (( SELECT count(1) AS zcount,
        //          zm_buildings.xzq,
        //          zm_buildings.xzqdm
        //         FROM (select  t1.xzq,t1.xzqdm,fwzt from zm_buildings t1 LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text) zm_buildings  where fwzt is null
        //        GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
        //        ORDER BY zm_buildings.xzqdm) t1
        //   LEFT JOIN ( 	SELECT 
        //          count(DISTINCT
        //              CASE
        //                  WHEN (zm_pc_base.xggdjsbp > 0) THEN zm_pc_base.fwbh
        //                  ELSE NULL::character varying
        //              END) AS xggdjsbpds,      
        //          zm_pc_base.xzqdm
        //         FROM (select t1.create_on,t1.xzqdm,t1.xggdjsbp,t1.fwbh,t2.fwzt From zm_pc_yh t0  join zm_pc_baselog t1 on t0.fwbh=t1.fwbh LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh where  t1.create_on  between @startime and @endtime)zm_pc_base
        //        WHERE  create_on between @startime and @endtime and fwzt is null
        //        GROUP BY zm_pc_base.xzqdm) t2 ON (((t1.xzqdm)::text = (t2.xzqdm)::text))) 
        //ORDER BY t1.xzqdm)a  ORDER BY value desc,xzqdm) a   ";
        //          param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
        //          var lea = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Length - 3 : param.code.Length;
        //          if (lea == 6 && isxz)
        //          {
        //              sql = @$" select  xggdjsbpds,zcount,code,town AS name,case when value >100 then 100.00 when value is null then 0.00 else value end value from (  select zcount,xggdjsbpds,round(xggdjsbpds*1.00*100/zcount,2) value ,town,towndm code from  (  SELECT t1.xzq,
        //  t1.xzqdm,
        //  t3.region_name AS town,
        //  t1.towndm,
        //  COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
        //  COALESCE(t2.xggdjsbpds, (0)::bigint) AS xggdjsbpds
        // FROM ((( SELECT count(1) AS zcount,
        //          zm_buildings.towndm,
        //          zm_buildings.xzq,
        //          zm_buildings.xzqdm
        //         FROM (select t1.towndm,t1.xzq,t1.xzqdm,t1.town,fwzt from zm_buildings t1 LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text where fwzt is null) zm_buildings  
        //        GROUP BY zm_buildings.towndm, zm_buildings.xzq, zm_buildings.xzqdm
        //        ORDER BY zm_buildings.towndm) t1
        //   LEFT JOIN ( SELECT 
        //          count(DISTINCT
        //              CASE
        //                  WHEN (zm_pc_base.xggdjsbp > 0) THEN zm_pc_base.fwbh
        //                  ELSE NULL::character varying
        //              END) AS xggdjsbpds,      
        //          zm_pc_base.town
        //         FROM (select t1.xzq,t1.create_on,t1.xzqdm,t1.town,t1.village,t1.xggdjsbp,t1.fwbh,t2.fwzt From zm_pc_yh t0  join zm_pc_baselog t1 on t0.fwbh=t1.fwbh LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh where  t1.create_on  between @startime and @endtime)zm_pc_base
        //        WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
        //                 FROM zm_buildings)) and  create_on between @startime and @endtime and fwzt is null
        //        GROUP BY zm_pc_base.town) t2 ON (((t1.towndm)::text = (t2.town)::text)))
        //   LEFT JOIN zm_department t3 ON (((t1.towndm)::text = (t3.id)::text))))a where towndm like @code  ORDER BY value desc,towndm) a  ";
        //          }
        //          if (lea == 9)
        //          {
        //              sql = @$" select  xggdjsbpds,zcount,code,village AS name,case when value >100 then 100.00 when value is null then 0.00 else value end value from (  select zcount,xggdjsbpds ,round(xggdjsbpds*1.00*100/zcount,2) value ,village,villagedm code from  (  SELECT t4.region_name AS village,
        //  t1.villagedm,
        //  COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
        //  COALESCE(t2.xggdjsbpds, (0)::bigint) AS xggdjsbpds
        // FROM (((( SELECT count(1) AS zcount,
        //          zm_buildings.villagedm
        //         FROM (select t1.*,fwzt from zm_buildings t1 LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text) zm_buildings  where fwzt is null
        //        GROUP BY zm_buildings.villagedm
        //        ORDER BY zm_buildings.villagedm) t1
        //   LEFT JOIN ( SELECT 
        //          count(DISTINCT
        //              CASE
        //                  WHEN (zm_pc_base.xggdjsbp > 0) THEN zm_pc_base.fwbh
        //                  ELSE NULL::character varying
        //              END) AS xggdjsbpds,
        //          zm_pc_base.village 
        //         FROM  (select t1.create_on,t1.village,t1.xggdjsbp,t1.fwbh,t2.fwzt From zm_pc_yh t0  join zm_pc_baselog t1 on t0.fwbh=t1.fwbh LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh where  t1.create_on  between @startime and @endtime)zm_pc_base
        //        WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
        //                 FROM zm_buildings)) and create_on between @startime and @endtime and fwzt is null
        //        GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
        //   LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text))))
        //ORDER BY t1.villagedm)a where villagedm like @code  ORDER BY value desc,villagedm) a 
        //        "; }
        //          if (lea == 12)
        //          {
        //              sql = @$" select  xggdjsbpds,zcount,code,village AS name,case when value >100 then 100.00 when value is null then 0.00 else value end value from (  select zcount,xggdjsbpds ,round(xggdjsbpds*1.00*100/zcount,2) value ,village,villagedm code from  (  SELECT t4.region_name AS village,
        //  t1.villagedm,
        //  COALESCE((t1.zcount)::numeric, (0)::numeric) AS zcount,
        //  COALESCE(t2.xggdjsbpds, (0)::bigint) AS xggdjsbpds
        // FROM (((( SELECT count(1) AS zcount,
        //          zm_buildings.villagedm
        //         FROM (select t1.*,fwzt from zm_buildings t1 LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh::text = t2.fwbh::text) zm_buildings  where fwzt is null
        //        GROUP BY zm_buildings.villagedm
        //        ORDER BY zm_buildings.villagedm) t1
        //   LEFT JOIN ( SELECT 
        //          count(DISTINCT
        //              CASE
        //                  WHEN (zm_pc_base.xggdjsbp > 0) THEN zm_pc_base.fwbh
        //                  ELSE NULL::character varying
        //              END) AS xggdjsbpds,
        //          zm_pc_base.village 
        //         FROM  (select t1.create_on,t1.village,t1.xggdjsbp,t1.fwbh,t2.fwzt From zm_pc_yh t0  join zm_pc_baselog t1 on t0.fwbh=t1.fwbh LEFT JOIN zm_pc_base_cj t2 ON t1.fwbh = t2.fwbh where  t1.create_on  between @startime and @endtime)zm_pc_base
        //        WHERE ((zm_pc_base.village)::text IN ( SELECT zm_buildings.villagedm
        //                 FROM zm_buildings)) and create_on between @startime and @endtime and fwzt is null
        //        GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text)))
        //   LEFT JOIN zm_department t4 ON (((t1.villagedm)::text = (t4.id)::text))))
        //ORDER BY t1.villagedm)a where villagedm like @code  ORDER BY value desc,villagedm) a 
        //        ";
        //          }
        //          var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
        //          var res = new RespRet<object>(true, "Ok", msg);
        //          WriteSystemLog("housescj/wgsyyhsyqbar", res, param.code);
        //          return new RespRet<object>(true, "Ok", msg);
        //      }

        /// <summary>
        /// 各区自查栋数，自查次数
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/zcqk")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> Zcqk([FromBody] DateQuery param)
        {
            try
            {
                if (param == null || param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "请传递时间");
                }
                var usernNumber = this.CurrentUser();
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                bool isxz = true;
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
                string sql = @" ";
                if (param.code.Length == 3 || param.code.Length == 6)
                {
                    sql = @" select a.zccount,a.xzqdm code,a.xzq name,case when  fwzccs is null then 0 else fwzccs end fwzccs   From zcqkgqtext(@startime,@endtime) a left join
						 (select count(1) fwzccs,b.xzqdm from zm_pc_yhlog a join zm_buildings b on a.fwbh=b.fwbh left join zm_pc_base_cj c on b.fwbh=c.fwbh where  a.create_on<(case when c.fwzt is not null then c.create_on else now() end)  and date(a.create_on) between @startime and @endtime  GROUP BY b.xzqdm) b on a.xzqdm=b.xzqdm   ";
                }

                if (isxz)
                {
                    if (param.code.Length == 6)
                    {
                        sql = @"   select a.zccount,a.towndm code,a.town name,case when  fwzccs is null then 0 else fwzccs end fwzccs  From zcqkxztext(@startime,@endtime) a left join
						 (select count(1) fwzccs,zm_buildings.towndm from zm_pc_yhlog join zm_buildings on zm_pc_yhlog.fwbh=zm_buildings.fwbh  left join zm_pc_base_cj c on zm_buildings.fwbh=c.fwbh where  zm_pc_yhlog.create_on<(case when c.fwzt is not null then c.create_on else now() end) and date(zm_pc_yhlog.create_on) between date(@startime) and date(@endtime)  GROUP BY zm_buildings.towndm) b on a.towndm=b.towndm where a.towndm like @code    ";
                    }

                }

                if (param.code.Length == 9)
                {
                    sql = @" select a.zccount,a.villagedm code,a.village name,case when  fwzccs is null then 0 else fwzccs end fwzccs  From zcqkcjtext(@startime,@endtime) a left join
						 (select count(1) fwzccs,zm_buildings.villagedm from zm_pc_yhlog join zm_buildings on zm_pc_yhlog.fwbh=zm_buildings.fwbh left join zm_pc_base_cj c on zm_buildings.fwbh=c.fwbh where  zm_pc_yhlog.create_on<(case when c.fwzt is not null then c.create_on else now() end)  and date(zm_pc_yhlog.create_on) between @startime and @endtime  GROUP BY zm_buildings.villagedm) b on a.villagedm=b.villagedm  where a.villagedm like @code    ";
                }
                if (param.code.Length == 12)
                {
                    sql = @" select a.zccount,a.villagedm code,a.village name,case when  fwzccs is null then 0 else fwzccs end fwzccs  From zcqkcjtext(@startime,@endtime) a left join
						 (select count(1) fwzccs,zm_buildings.villagedm from zm_pc_yhlog join zm_buildings on zm_pc_yhlog.fwbh=zm_buildings.fwbh left join zm_pc_base_cj c on zm_buildings.fwbh=c.fwbh where  zm_pc_yhlog.create_on<(case when c.fwzt is not null then c.create_on else now() end)  and date(zm_pc_yhlog.create_on) between @startime and @endtime  GROUP BY zm_buildings.villagedm) b on a.villagedm=b.villagedm  where a.villagedm like @code    ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<ZcqkDto>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
                var zfwzccs = data.Sum(c => c.fwzccs);

                var res = new RespRet<object>(true, "Ok", new { data = data, zfwzccs = zfwzccs });
             //   WriteSystemLog("housescj/zcqk", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/zcqk", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }



        /// <summary>
        /// 车辆与充电设施配比情况
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>

        [HttpPost("housescj/clycdsspbqk")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]
        public RespRet<object> clycdsspbqk([FromBody] DateQuery param)
        {
            var usernNumber = this.CurrentUser();
            if (param.date == null || param.date.Count != 2)
            {
                return new RespRet<object>(false, "时间不能为空");
            }

            if (param.date[1] > DateTime.Now)
            {
                param.date[1] = DateTime.Today;
            }
            param.date[1] = param.date[1].AddHours(23);
            param.date[1] = param.date[1].AddMinutes(59);
            param.date[1] = param.date[1].AddSeconds(59);
            string formattedDate = param.date[1].ToString("yyyy-MM-dd HH:mm:ss");
            string table_name = "zm_building_fwzt_" +DateTime.Now.Date.Year;
            var isxz = true;
            var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
            var a = JsonRegion(user.District);
            JObject obj = JObject.Parse(user.District);
            string RegionCode = (string)obj["RegionCode"];
            RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
            if (string.IsNullOrWhiteSpace(param.code))
            {
                isxz = false;
                param.code = RegionCode;
            }
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            string sql = @$" select *from				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,count(case when sfsqfwftcf=1 then 1 end) sqfwfsl  from zm_pc_gjzz_cj WHERE  date(create_on) between @startime and @endtime::TIMESTAMP)  t1
,			 				
(select COALESCE(sum(a.czryrs),0)czryrs from zm_pc_base a where a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           ) and     a.create_on between @startime and @endtime::TIMESTAMP  ) t2  ";
            string sqldata = @$" select t1.*,bdfwjzrydddzxcsl,COALESCE(cdjksl,0) cdjksl,COALESCE(sqfwfsl,0) sqfwfsl,COALESCE(xy,0) xy,COALESCE(xd,0) xd,COALESCE(dy,0) dy from (SELECT DISTINCT zm_buildings.xzqdm code,
            zm_buildings.xzq name
           FROM zm_buildings) t1 left join				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,
count(case when sfsqfwftcf=1 then 1 end) sqfwfsl,xzqdm,
count(case when cddf=3 then 1 end)+count(case when bjcdssdfqk=3 then 1 end) xy,
count(case when cddf=2 then 1 end)+count(case when bjcdssdfqk=2 then 1 end) xd,
count(case when cddf=1 then 1 end)+count(case when bjcdssdfqk=1 then 1 end) dy
from zm_pc_gjzz_cj WHERE  create_on between @startime and @endtime  GROUP BY xzqdm) t2 on t1.code
=t2.xzqdm       left join 
 ( SELECT t1.xzqdm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
            zm_buildings.xzq,
            zm_buildings.xzqdm
           FROM 					zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )
          GROUP BY zm_buildings.xzqdm, zm_buildings.xzq
          ORDER BY zm_buildings.xzqdm) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.xzq,
            zm_pc_base.xzqdm
           FROM  (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE   fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )	
          GROUP BY zm_pc_base.xzqdm, zm_pc_base.xzq) t2 ON (((t1.xzqdm)::text = (t2.xzqdm)::text))) left join (select t1.xzqdm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh where t1.create_on between @startime and @endtime and fwzt is null GROUP BY t1.xzqdm) t3 on t2.xzqdm=t3.xzqdm) t3 on t1.code=t3.xzqdm ORDER BY t3.xzqdm ";
            param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;
            var lea = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Length - 3 : param.code.Length;
            if (lea == 6 && isxz)
            {
                sqldata = @$" select t1.*,bdfwjzrydddzxcsl,COALESCE(cdjksl,0) cdjksl,COALESCE(sqfwfsl,0) sqfwfsl,COALESCE(xy,0) xy,COALESCE(xd,0) xd,COALESCE(dy,0) dy from (
  SELECT DISTINCT a.towndm code,b.region_name name FROM zm_buildings a,zm_department b where a.towndm=b.region_code and b.regional_level='6'
 ) t1 left join				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,
count(case when sfsqfwftcf=1 then 1 end) sqfwfsl,towndm,
count(case when cddf=3 then 1 end)+count(case when bjcdssdfqk=3 then 1 end) xy,
count(case when cddf=2 then 1 end)+count(case when bjcdssdfqk=2 then 1 end) xd,
count(case when cddf=1 then 1 end)+count(case when bjcdssdfqk=1 then 1 end) dy
from zm_pc_gjzz_cj WHERE  create_on between @startime and @endtime  GROUP BY towndm) t2 on t1.code
=t2.towndm left join 
 ( SELECT t1.towndm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.town),
            zm_buildings.towndm
           FROM 					zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )
          GROUP BY zm_buildings.towndm
          ORDER BY zm_buildings.towndm) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.town
           FROM  (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE   fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )	
          GROUP BY zm_pc_base.town) t2 ON (((t1.towndm)::text = (t2.town)::text))) left join (select t1.towndm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh where t1.create_on between @startime and @endtime and fwzt is null GROUP BY t1.towndm) t3 on t1.towndm=t3.towndm ) t3 on t1.code=t3.towndm where t1.code like @code ";
            }
            if (lea == 9)
            {
                sql = @$" 					
				select t1.*,COALESCE(cdjksl,0) cdjksl,COALESCE(sqfwfsl,0) sqfwfsl,COALESCE(czryrs,0)czryrs from (
  SELECT DISTINCT a.towndm code,b.region_name name FROM zm_buildings a,zm_department b where a.towndm=b.region_code and b.regional_level='6'
 ) t1 left join				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,count(case when sfsqfwftcf=1 then 1 end) sqfwfsl,towndm  from zm_pc_gjzz_cj WHERE  create_on between @startime and @endtime GROUP BY towndm) t2 on t1.code
=t2.towndm
	left join 				 				
(select  sum(a.czryrs)czryrs,a.town towndm from zm_pc_base a where a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )    and a.create_on between @startime and @endtime GROUP BY a.town) t3 on t1.code=t3.towndm where t1.code like @code 	
					
 ";
                sqldata = @$" select t1.*,bdfwjzrydddzxcsl,COALESCE(cdjksl,0) cdjksl,COALESCE(sqfwfsl,0) sqfwfsl,COALESCE(xy,0) xy,COALESCE(xd,0) xd,COALESCE(dy,0) dy from (SELECT DISTINCT a.villagedm code,b.region_name name FROM zm_buildings a,zm_department b where a.villagedm=b.region_code and b.regional_level='7'
 ) t1 left join				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,
count(case when sfsqfwftcf=1 then 1 end) sqfwfsl,villagedm,
count(case when cddf=3 then 1 end)+count(case when bjcdssdfqk=3 then 1 end) xy,
count(case when cddf=2 then 1 end)+count(case when bjcdssdfqk=2 then 1 end) xd,
count(case when cddf=1 then 1 end)+count(case when bjcdssdfqk=1 then 1 end) dy
from zm_pc_gjzz_cj WHERE  date(create_on) between @startime and @endtime  GROUP BY villagedm) t2 on t1.code
=t2.villagedm left join 
 ( SELECT t1.villagedm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.village),
            zm_buildings.villagedm
           FROM 					zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )
          GROUP BY zm_buildings.villagedm
          ) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.village
           FROM 			 (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE   fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )	
          GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text))) left join (select t1.villagedm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh where t1.create_on between @startime and @endtime and fwzt is null GROUP BY t1.villagedm) t3 on t1.villagedm=t3.villagedm
          ) t3 on t1.code=t3.villagedm where t1.code like @code ";

            }
            if (lea == 12)
            {
                sql = @$" 					
				select t1.*,COALESCE(cdjksl,0) cdjksl,COALESCE(sqfwfsl,0) sqfwfsl,COALESCE(czryrs,0)czryrs from (
  SELECT DISTINCT a.towndm code,b.region_name name FROM zm_buildings a,zm_department b where a.towndm=b.region_code and b.regional_level='6'
 ) t1 left join				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,count(case when sfsqfwftcf=1 then 1 end) sqfwfsl,towndm  from zm_pc_gjzz_cj WHERE  create_on between @startime and @endtime GROUP BY towndm) t2 on t1.code
=t2.towndm
	left join 				 				
(select  sum(a.czryrs)czryrs,a.town towndm from zm_pc_base a where a.fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )    and a.create_on between @startime and @endtime GROUP BY a.town) t3 on t1.code=t3.towndm where t1.code like @code 	
					
 ";
                sqldata = @$" select t1.*,bdfwjzrydddzxcsl,COALESCE(cdjksl,0) cdjksl,COALESCE(sqfwfsl,0) sqfwfsl,COALESCE(xy,0) xy,COALESCE(xd,0) xd,COALESCE(dy,0) dy from (SELECT DISTINCT a.villagedm code,b.region_name name FROM zm_buildings a,zm_department b where a.villagedm=b.region_code and b.regional_level='7'
 ) t1 left join				 			 			 
(select COALESCE(sum(qcxycdjk),0)+COALESCE(sum(tzzfybjcdssjk),0)  cdjksl,
count(case when sfsqfwftcf=1 then 1 end) sqfwfsl,villagedm,
count(case when cddf=3 then 1 end)+count(case when bjcdssdfqk=3 then 1 end) xy,
count(case when cddf=2 then 1 end)+count(case when bjcdssdfqk=2 then 1 end) xd,
count(case when cddf=1 then 1 end)+count(case when bjcdssdfqk=1 then 1 end) dy
from zm_pc_gjzz_cj WHERE  date(create_on) between @startime and @endtime  GROUP BY villagedm) t2 on t1.code
=t2.villagedm left join 
 ( SELECT t1.villagedm,
    COALESCE(t2.bdfwjzrydddzxcsl, (0)::bigint) AS bdfwjzrydddzxcsl
   FROM (( SELECT count(1) AS zcount,
           max(zm_buildings.village),
            zm_buildings.villagedm
           FROM 					zm_buildings where  fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )
          GROUP BY zm_buildings.villagedm
          ) t1
     LEFT JOIN ( SELECT 
            sum(zm_pc_base.bdfwjzrydddzxcsl) AS bdfwjzrydddzxcsl,
            zm_pc_base.village
           FROM  (select t1.*  From zm_pc_yh t0  join zm_pc_base t1 on t0.fwbh=t1.fwbh  where t1.create_on between @startime and @endtime::TIMESTAMP)zm_pc_base
          WHERE   fwbh not in (
             SELECT DISTINCT fwbh FROM {table_name} WHERE final_fwzt IS NOT NULL AND final_create_on =@formattedDate
           )	
          GROUP BY zm_pc_base.village) t2 ON (((t1.villagedm)::text = (t2.village)::text))) left join (select t1.villagedm,count(distinct t1.fwbh) zccount from zm_pc_yhlog t1 left join zm_pc_base_cj t2 on t1.fwbh=t2.fwbh where t1.create_on between @startime and @endtime and fwzt is null GROUP BY t1.villagedm) t3 on t1.villagedm=t3.villagedm
          ) t3 on t1.code=t3.villagedm where t1.code like @code ";
            }
            var msg = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1],@formattedDate=formattedDate }).FirstOrDefault();
            var data = _dbContext.Database.GetDbConnection().Query<object>(sqldata, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1], @formattedDate = formattedDate }).ToList();
            return new RespRet<object>(true, "Ok", new { msg = msg, data = data });
        }



        /// <summary>
        /// 是否安装软梯逃生绳等辅助逃生设施、楼内有无固定楼梯通向屋顶字段统计
        /// </summary>
        /// <param name="pageRequest"></param>
        /// <returns></returns>
        [HttpPost("housescj/fztsss")]
        [Produces("application/json")]
        [ProducesResponseType(typeof(RespRet<object>), 200)]

        public RespRet<object> TSsstj([FromBody] DateQuery param)
        {
            try
            {
                var usernNumber = this.CurrentUser();
                var user = _dbContext.ZmUser.FirstOrDefault(c => c.Number == usernNumber);
                var a = JsonRegion(user.District);
                JObject obj = JObject.Parse(user.District);
                string RegionCode = (string)obj["RegionCode"];
                RegionCode = RegionCode.Substring(RegionCode.Length - 3) == "000" ? RegionCode.Substring(0, RegionCode.Length - 3) : RegionCode;
                var isxz = true;
                if (param.date == null || param.date.Count != 2)
                {
                    return new RespRet<object>(false, "时间不能为空");
                }
                param.date[1] = param.date[1].AddHours(23);
                param.date[1] = param.date[1].AddMinutes(59);
                param.date[1] = param.date[1].AddSeconds(59);
                if (string.IsNullOrWhiteSpace(param.code))
                {
                    isxz = false;
                    param.code = RegionCode;
                }
                param.code = param.code.Substring(param.code.Length - 3) == "000" ? param.code.Substring(0, param.code.Length - 3) : param.code;

                string sql = @" ";
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                string starttime = Convert.ToDateTime(param.date[0] + "").ToString("yyyy-MM-dd");
                string endtime = Convert.ToDateTime(param.date[1] + "").ToString("yyyy-MM-dd");
                string tablename = "";
                string filed = "";
                string fileWhere = "";
                if ( endtime + "" == today)
                {
                    tablename = "zm_pc_base";
                }
                else
                {
                    string startimemonth = Convert.ToDateTime(param.date[0] + "").ToString("yyyy_MM");
                    string endmonth = Convert.ToDateTime(param.date[1] + "").ToString("yyyy_MM");
                    string month = "";
                    if (startimemonth == endmonth)
                    {
                        month = startimemonth;
                        tablename = "zm_pc_baselog_"+ month;
                        filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                        fileWhere = " and row_num=1 ";
                    }
                    else 
                    {
                        tablename = "zm_pc_baselog";
                        filed = "ROW_NUMBER() OVER(PARTITION BY t1.fwbh ORDER BY t1.create_on DESC) AS row_num,";
                        fileWhere = " and row_num=1 ";
                    }
                }

                if (param.code.Length == 3 || param.code.Length == 6)
                {
                    sql = " select * from  fztsss_gq_text('" + filed + "','" + fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','"+ tablename + "') ";
                }

                if (param.code.Length == 6 && isxz)
                {
                    sql = " select * from  fztsss_xz_text('"+ filed + "','"+ fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','"+ tablename + "','%" + param.code + "%') ";
                }

                if (param.code.Length == 9 || param.code.Length == 12)
                {
                    sql = " select * from  fztsss_cj_text('"+ filed + "','"+ fileWhere + "','" + starttime + " 00:00:00','" + endtime + " 23:59:59','"+ tablename + "','%" + param.code + "%') ";
                }
                var data = _dbContext.Database.GetDbConnection().Query<object>(sql, new { @code = "%" + param.code + "%", @startime = param.date[0], @endtime = param.date[1] }).ToList();
                var res = new RespRet<object>(true, "Ok", data);
               // WriteSystemLog("housescj/fztsss", res, null);
                return res;
            }
            catch (Exception ex)
            {
                var res = new RespRet<object>(false, ex.ToString());
                WriteSystemLogError("housescj/fztsss", res, null);
                res.Msg = "系统异常";
                return res;
            }
        }
    }
}
